A Lecture Number 4
A Lecture Number 4
objects
Apple
Banana
Mango
Another example:
class
Car
objects
Volvo
Audi
Toyota
When the individual objects are created, they inherit all the
variables and methods from the class.
You will learn much more about classes and objects in the next
chapter.
Create a Class
To create a class, use the keyword class:
Main.java
Create an Object
In Java, an object is created from a class. We have already
created the class named Main, so now we can use this to create
objects.
Multiple Objects
You can create multiple objects of one class:
Example
Remember that the name of the java file should match the class
name. In this example, we have created two files in the same
directory/folder:
• Main.java
• Second.java
1. Main.java
public class Main {
int x = 5;
}
2. Second.java
class Second {
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Example
Accessing Attributes
You can access attributes by creating an object of the class, and
by using the dot syntax (.):
Example
Modify Attributes
You can also modify attribute values:
Example
Example
Example
public class Main {
final int x = 10;
Multiple Objects
If you create multiple objects of one class, you can change the
attribute values in one object, without affecting the attribute
values in the other:
Example
Multiple Attributes
You can specify as many attributes as you want:
Example
public class Main {
String fname = "John";
String lname = "Doe";
int age = 24;
The next chapter will teach you how to create class methods and
how to access them with objects.
Example
Example
Example
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by
creating objects");
}
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
Remember that..
The dot (.) is used to access the object's attributes and methods.
• Main.java
• Second.java
3. Main.java
public class Main {
public void fullThrottle() {
System.out.println("The car is going as fast as it
can!");
}
4. Second.java
class Second {
public static void main(String[] args) {
Main myCar = new Main(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle()
method
myCar.speed(200); // Call the speed() method
}
}
Java Constructors
Java Constructors
A constructor in Java is a special method that is used to
initialize objects. The constructor is called when an object of a
class is created. It can be used to set initial values for object
attributes:
Example
Create a constructor:
// Create a Main class
public class Main {
int x; // Create a class attribute
// Outputs 5
Note that the constructor name must match the class name, and
it cannot have a return type (like void).
Constructor Parameters
Constructors can also take parameters, which is used to initialize
attributes.
Example
public class Main {
int x;
public Main(int y) {
x = y;
}
// Outputs 5
Example
public class Main {
int modelYear;
String modelName;