Classes, Objects & Methods Flashcards
Class Syntax
class Name {
}
When is a main method required in a class?
If the class is the starting point of you program.
What types of applications don’t require a main method?
Applets
How do you access instance variables from an object?
object.variable
How does the Java compiler sort class definitions?
It puts each class into its own .class file.
Is it necessary for classes to be in the same source file?
No.
Do the contents of the variables in one object differ from another?
Yes, each instance of a class stores its own contents.
Object Declaration Syntax
object Name = new object();
What does the new operator do?
dynamically allocates memory for an object and returns a reference to it.
Reference Variables
Variables referring to objects.
Vehicle car1 = new Vehicle(); Vehicle car2 = car1;
Are car1 and car2 referencing the same or different objects?
Same
What are methods used for?
Manipulating and providing access to data inside classes.
What method begins execution of a program?
The main method
public static void main(String []args)
{
}
Method Syntax
type name(param) {
}
When is the method Return type void?
If the method doesn’t return a value.
How can you cause the immediate termination of a void method prior to its ending brace? What happens to program control?
Return;
Program control returns to the caller.
How to return a value in a method?
return value;
What methods won’t return values?
Void methods
What method will initialize an object, provide initial values to instance variables & perform other startup procedures?
A constructor
What do you name a constructor?
The same name as its class
What return type should a constructor have?
It has no return type
Do all classes have constructors?
Yes, even if not defined, due to a default constructor in the Object class that initializes all member variables to their default values.
What are the default values of all member variables?
Zero, null & false
Can you add parameters to constructors?
Yes