Calling Class Methods - AP Computer Science A
- What happens if you call a static method with a null reference?
- No error; static method is called on class. Static methods belong to class, not instance.
- How do you call a static method from a different package?
- Import the class, then call ClassName.methodName();. Import statement provides access to external classes.
- Identify the error: public void static print() {}
- Incorrect order; should be public static void print() {}. Modifiers must follow specific order: access, static, return.
- What type of methods can be called without creating an object?
- Static methods. Class methods don't require object instantiation.
- Identify the error: static void display() { this.show(); }
- Cannot use 'this' in a static method. 'this' refers to instance, not available in static context.
- Can a static method call a non-static method directly?
- No, it cannot. Static context cannot access instance methods directly.
- What keyword is used to define a class method?
- static. Makes method belong to class rather than instances.
- How do you call a static method from a class?
- ClassName.methodName();. Uses dot notation with class name, no object needed.
- Can static methods be overridden?
- No, they can be hidden but not overridden. Static methods are resolved at compile-time, not runtime.
- What is the result of calling a non-static method using the class name?
- Compilation error. Non-static methods require an instance to be called.
- How do you call the main method of another class?
- OtherClass.main(args);. Main method is static, so use class name to call.
- Can a static method be overloaded?
- Yes, it can. Multiple static methods can have same name, different parameters.
- State the syntax for calling a static method in Java.
- ClassName.methodName();. Standard format for accessing class-level methods.
- Which method is called automatically when a Java program starts?
- main method. Entry point for Java program execution.
- Can a static method have a return type?
- Yes, it can. Static methods can return values like any method.
- Identify the error: static int calculate() return 0;
- Missing curly braces. Method body requires braces around statements.
- What is the signature of the main method in Java?
- public static void main(String[] args). Required format for Java application entry point.
- Can a static method be abstract?
- No, it cannot. Abstract requires overriding, but static prevents inheritance.
- Can a static method call another static method?
- Yes, it can. Static methods can invoke other static methods directly.
- What are the parameters of the main method in Java?
- String[] args. Command-line arguments passed to program.
- What is the scope of a static method?
- Class level. Belongs to the class, shared across all instances.
- Can static methods access instance variables directly?
- No, they cannot. Instance variables belong to objects, not the class.
- Can a static method access a static variable?
- Yes, it can. Static methods can access other static members.