Java Objects Flashcards
What is an object in Java?
An object is an instance of a class that holds data (fields) and behavior (methods).
class Dog { String name; // field to store the dog's name void bark() { // method to make the dog bark System.out.println(name + " says woof!"); } } Dog myDog = new Dog(); // create a new Dog object
How do you create an object in Java?
Use the new
keyword followed by the constructor of a class.
Dog myDog = new Dog(); // create a Dog object using the default constructor
How do you access fields and methods of an object?
Use the dot (.
) operator to access members of an object.
myDog.name = "Millie"; // assign value to the field myDog.bark(); // call the method
What is a constructor in Java?
A constructor is a special method that initializes an object when it’s created.
class Person { String name; // field Person(String n) { // constructor name = n; // initialize field } } Person p = new Person("Owen"); // create a new Person object
Can you overload constructors in Java?
Yes, you can define multiple constructors with different parameter lists.
class Cat { String name; int age; Cat(String n) { name = n; } // constructor with one parameter Cat(String n, int a) { name = n; age = a; } // constructor with two parameters }
What is the this
keyword used for in Java?
this
refers to the current object and helps distinguish instance variables from parameters.
class Book { String title; Book(String title) { this.title = title; // 'this.title' refers to the instance variable } }
What happens if an object is declared but not initialized?
Its reference is null
, and accessing it will cause a NullPointerException
.
Car car; // declared but not initialized car.drive(); // NullPointerException when calling a method on null reference
Can objects be passed to methods in Java?
Yes, Java passes the object reference by value.
void paint(Car c) { c.color = "red"; // modify the object's field } Car myCar = new Car(); paint(myCar); // myCar.color is now "red"
How do you compare two objects in Java?
Use .equals()
for content comparison and ==
for reference comparison.
String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1 == s2); // false - compares references System.out.println(s1.equals(s2)); // true - compares content
What is the default value of an object reference in Java?
The default value of an object reference is null
.
String name; // not initialized System.out.println(name); // prints null
How do you define a class that can create objects?
Define fields and methods inside a class.
class Student { String name; // field void study() { // method System.out.println(name + " is studying."); } }
Can objects be stored in arrays in Java?
Yes, you can store object references in arrays.
Student[] students = new Student[3]; // array of Student references students[0] = new Student(); // assign a Student object to index 0
What is the difference between a reference and an object?
A reference points to an object in memory, while the object holds the actual data.
Dog a = new Dog(); // 'a' is a reference to the Dog object
What is object encapsulation?
Encapsulation means keeping fields private and providing public methods to access them.
class BankAccount { private double balance; // private field public double getBalance() { return balance; } // public getter }
What is the null
keyword used for in Java?
null
represents the absence of a value for an object reference.
String s = null; // no object assigned to s
What are the Declaration, Instantiation, and Initialization steps in Java object creation?
All three steps involved in creating an object in Java:
1. Declaration – Declare a reference variable:
Car myCar;
2. Instantiation – Use new
to create the object in memory:
new
3. Initialization – The constructor initializes the object:
Car(); // Constructor call
Full example combining all three:
Car myCar = new Car();
-
Car myCar
is the declaration. -
new
is the instantiation. -
Car()
(constructor) is the initialization.