What is the difference between a class and an object?
A class is a blueprint for creating objects; an object is a particular instance created from that class.
Study Java Class Design Essentials with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is the difference between a class and an object?
A class is a blueprint for creating objects; an object is a particular instance created from that class.
How does an instance variable differ from a local variable?
An instance variable belongs to an object and persists with it; a local variable exists only inside the method or constructor where it is declared.
What default values does Java assign to uninitialized instance variables?
Uninitialized numeric instance variables default to 0 or 0.0, booleans to false, and reference types such as String to null.
What rules identify a Java constructor?
A constructor has the class’s name, no return type, runs when new creates an object, and may receive parameters.
What makes two constructors overloaded?
Constructor overloading means defining multiple constructors in one class with different parameter lists, varying in number or type.
When does Java stop supplying an automatic no-argument constructor?
If a class declares any constructor, Java does not automatically provide a no-argument constructor. Write one explicitly if it is needed.
What does this(...) do inside a constructor?
The call to this(...) invokes another constructor in the same class and must be the first statement in the calling constructor.
How do accessor and mutator methods differ?
An accessor, or getter, returns information without changing state. A mutator, or setter, changes the object’s state, potentially after validation.
What is encapsulation in class design?
Encapsulation combines data with its operations while restricting direct access, commonly using private fields and public methods that validate changes.
What does this refer to in an instance method?
In an instance method or constructor, this refers to the current object—the object whose code is executing.
Why write this.name = name in a constructor?
In this.name = name, this.name is the instance variable and name is the parameter. The qualification prevents the parameter from being assigned to itself.
How do static and instance variables differ?
A static variable has one shared copy for the class, whereas an instance variable has a separate copy in every object.