0% found this document useful (0 votes)
139 views20 pages

Understanding Java Class Constructors

1) A class is like a blueprint that defines the attributes and behaviors of an object. 2) To create an object from a class, use the new keyword followed by the class name. This calls the class's constructor. 3) Constructors can be default (no parameters) or parameterized (with parameters) and are used to initialize an object's attributes when the object is created.

Uploaded by

Shadrach zulu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views20 pages

Understanding Java Class Constructors

1) A class is like a blueprint that defines the attributes and behaviors of an object. 2) To create an object from a class, use the new keyword followed by the class name. This calls the class's constructor. 3) Constructors can be default (no parameters) or parameterized (with parameters) and are used to initialize an object's attributes when the object is created.

Uploaded by

Shadrach zulu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Java Classes/Objects

LECTURE
CLASSES
• Everything in Java is associated with classes and objects, along with
its attributes and methods. For example: in real life, a car is an object.
The car has attributes, such as weight and color, and methods, such
as drive and brake.
• A Class is like an object constructor, or a "blueprint" for creating
objects.
CLASSES
• To create a class, use the keyword class
Main.java//file name
• Create a class named "Main" with a variable x:
• public class Main {
• int x = 5;
•}
Creating Objects
• public class Main {
• int x = 5;

• public static void main(String[] args) {


• Main myObj = new Main();
• System.out.println(myObj.x);
• }
•}
Creating Multiple Objects
• public class Main {
• int x = 5;

• public static void main(String[] args) {


• Main myObj1 = new Main();
• Main myObj2 = new Main();
• System.out.println(myObj1.x);
• System.out.println(myObj2.x);
• }
•}
Creating Multiple Objects
• public class Main {
• int x = 5;

• public static void main(String[] args) {


• Main myObj1 = new Main();
• Main myObj2 = new Main();
• System.out.println(myObj1.x);
• System.out.println(myObj2.x);
• }
•}
Using Multiple Classes
• You can also create an object of a class and access it in another class.
This is often used for better organization of classes (one class has all
the attributes and methods, while the other class holds the main()
method (code to be executed)).
• 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:
Using Multiple Classes
• You can also create an object of a class and access it in another class.
This is often used for better organization of classes (one class has all
the attributes and methods, while the other class holds the main()
method (code to be executed)).
• 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
Using Multiple Classes
• You can also create an object of a class and access it in another class.
This is often used for better organization of classes (one class has all
the attributes and methods, while the other class holds the main()
method (code to be executed)).
• 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
Using Multiple Classes
Main.java
• public class Main {
• int x = 5;
• }
Second.java
• class Second {
• public static void main(String[] args) {
• Main myObj = new Main();
• System.out.println(myObj.x);
• }
• }
Exercise
• Create an object called “stud” of the class Student. When run, the
application should print a student name “Jane Doe”.
• TIP: Use string datatype to store the pre-assigned name value.
Java Constructors
LECTURE
Constructor
• 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.
• There are two rules defined for the constructor.
• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
Constructor
Types of Java constructors
• There are two types of constructors in Java:
• Default constructor (no-arg constructor)
• Parameterized constructor
Default Constructor
• A constructor is called "Default Constructor" when it doesn't have any
parameter.
• if there is no constructor in a class, compiler automatically creates a default
constructor.
Example
• // Create a Main class
• public class Main {
• int x; // Create a class attribute
• // Create a class constructor for the Main class
• public Main() {
• x = 5; // Set the initial value for the class attribute x
• }
• public static void main(String[] args) {
• Main myObj = new Main(); // Create an object of class Main (This will call the
constructor)
• System.out.println(myObj.x); // Print the value of x
• }
• }
• // Outputs 5
Constructor Parameters

• Constructors can also take parameters, which is used to initialize


attributes.
• The following example adds an int y parameter to the constructor.
Inside the constructor we set x to y (x=y). When we call the
constructor, we pass a parameter to the constructor (5), which will set
the value of x to 5:
Parameterized Constructor

• Constructors can also take parameters, which is used to initialize


attributes.
• The following example adds an int y parameter to the constructor.
Inside the constructor we set x to y (x=y). When we call the
constructor, we pass a parameter to the constructor (5), which will set
the value of x to 5:
Example 1

• public class Main {


• int x;

• public Main(int y) { // constructor with a parameter


• x = y;
• }
• public static void main(String[] args) {
• Main myObj = new Main(5); //passing value to the constructor
• System.out.println(myObj.x);
• }
• }
• // Outputs 5
Example 2

• public class Main {


• int modelYear;
• String modelName;
• public Main(int year, String name) {
• modelYear = year;
• modelName = name;
• }

• public static void main(String[] args) {


• Main myCar = new Main(1969, "Mustang");
• System.out.println(myCar.modelYear + " " + myCar.modelName);
• }
• }

• // Outputs 1969 Mustang

You might also like