Instance variable and static variable comparison using program

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

1132

Program:

// instance variable and static variable comparison using program
public class Dog1 {
	int rollNo = 1;   // this is instance variable
	static int age = 12; // this is static variable
	
	
	public static void main(String args[])
	{
		// to access instance variable we have to create object
		Dog1 dg = new Dog1();
		System.out.println(dg.rollNo);
		
		// to access the static variable no need to create object
		// we can access it directly
		System.out.println(age);
		
		
	}

}

Output:

1
12

Explanation:

Instance variable and static variable comparison using program:
// to access instance variable we have to create object
		Dog1 dg = new Dog1();
		System.out.println(dg.rollNo);

To access instance variable we have to create object

To access the static variable no need to create object. We can access it directly

// we can access it directly
		System.out.println(age);

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.