Monday, April 28, 2025

Java OO Tutorial - Constructors

A constructor is a special method that is used to create objects of a class. It is called automatically when an object is created using the "new" keyword. The constructor has the same name as the class and does not have a return type, not even void. 

public class Main {
	
	int x; 
	
	//This is Constructor - same name as Class name
	public Main() {
		x = 10;
	}	
			
	public static void main(String[] args) {
		
		Main newObj = new Main();
		
		System.out.println(newObj.x);
		
		newObj.x = 555;
		
		System.out.println(newObj.x);					
	}	
}

Here's a block-by-block breakdown of the program: 

public class Main {
    int x;

    public Main() {
        x = 10;
    }

In this block, we define a class named "Main" with a single member variable "x". We also define a constructor for this class that sets the value of "x" to 10. 

public static void main(String[] args) {
        Main newObj = new Main();

        System.out.println(newObj.x);

        newObj.x = 555;

        System.out.println(newObj.x);
    }

In this block, we define the main method which is the entry point for the program. Inside this method, we create an instance of the Main class named "newObj" using the constructor. We then print out the value of "x" using "System.out.println(newObj.x);", which will output "10" since that is the value set by the constructor. We then set the value of "x" to 555 and print it out again, which will output "555".

When we run this program, it will output the following to the console: 

10
555

This program demonstrates how a constructor can be used to set initial values for member variables when an object is created using the "new" keyword. In this case, the constructor sets the initial value of "x" to 10. 

public class Main {
	
	int x;
	String someString;
	
	//This is Constructor - same name as Class name
	public Main(int a, String b) {
		x = a;
		someString = b;
	}	
	
	public static void main(String[] args) {

		Main newObj = new Main(777, "hack The Planet");
		
		System.out.println("X value: " + newObj.x + " String Value: " + newObj.someString);	
	}	
}

Here's a block-by-block breakdown of the program: 

public class Main {
    int x;
    String someString;

    public Main(int a, String b) {
        x = a;
        someString = b;
    }

In this block, we define a class named "Main" with two member variables "x" and "someString". We also define a constructor for this class that takes two parameters, "a" and "b", which are used to set the values of "x" and "someString", respectively. 

public static void main(String[] args) {
        Main newObj = new Main(777, "hack The Planet");

        System.out.println("X value: " + newObj.x + " String Value: " + newObj.someString);
    }

In this block, we define the main method which is the entry point for the program. Inside this method, we create an instance of the Main class named "newObj" using the constructor and passing the arguments 777 and "hack The Planet". We then print out the value of "x" and "someString" using "System.out.println()", which will output "X value: 777 String Value: hack The Planet".

When we run this program, it will output the following to the console: 

X value: 777 String Value: hack The Planet

This program demonstrates how a constructor with parameters can be used to set initial values for member variables when an object is created using the "new" keyword with specific arguments. In this case, the constructor takes two arguments, an integer and a string, and sets the initial values of "x" and "someString" to the values passed as arguments.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...