Class Structure in java

In this section we will discuss about some fundamental concept of class structure in java.

Objective:

  • Structure of Class
  • Types of variables
  • Local variable
  • Instance variable
  • Static variable

Structure of Class:

In the earlier section we have discussed about the concept of operators, operand, conditional statement, Looping etc. All these are the basic concept in every programming language.

Now this is the time to understand the basic structure related to Java programming language. In java, the basic concept is Class.

In Class, we have several components. We can use all these components to define the class Such as

  • Variables
  • Methods
  • Blocks etc.

All the above component should be included inside the class. Object will be created through this class and all these codes will be accessed by that object.

Addition to these, we also have several types of variables, methods like static variables. Overloaded methods, overridden methods, static blocks etc.

Let’s discuss more about the variables, Methods & Blocks in details.

Variables in Java:

The first component in class structure in java is variables.

Variable is like a container which can hold the values to store and perform some operation. Stack area is used to save all variable in java. Those variables which can hold the object (basically this is not 100% variable) is stored in heap area. Let’s discuss on the variables in details

Class Structure in java

In Java, we have generally 3 types of variables.

  • Local Variable
  • Instance Variable
  • Static Variable

Let’s discuss on each variable types in briefly.

Local Variable:

Variable which is defined inside the function / method is known as Local variable in java. The scope of the variable is within the function/ methods only. Outside the function / method it has not value. Now we will understand this with a simple example.

class Checklocal {
	public static void main(String args[])
	{
		int a = 4;
		void localcheck{
			int b = 8; 
			System.out.println(a);
		}
		System.out.println(b);		
	}
}

Output:

But why?

This error is generally like a compilation error. This signifies that you want to show some variable in System.out.println() which is not defined.

Now you can as we have defined the variable b in line number 6. Then why this is showing to declare the variable again.

Yes, you have declared the variable b in line 6 but that is inside the block. So it is considering as a local variable. As a result when you are trying to print the value of b outside of the block, the is showing compilation error with Unresolved compilation problem: b cannot be resolved to a variable.

Now let’s check another scenario in local variable.

Example:

class Checklocal {
	public static void main(String args[])
	{
		int a;
		System.out.println(a);	
	}
}

Output:

In this example, we have defined the local variable, but have not initialized. Because of that we are seeing the error.

Note: Local variable should be initialized and should not be used outside of the method / function in which this is defined.

Example:

Class Checklocal {
	public static void main(String args[])
	{
		int a = 5;
		System.out.println(a);
	}
}

Output:

5

Here we have defined and initialized the local variable which print the output as “5”.

Instance Variable:

After understanding local variable, let’s discuss on Instance variable.

The variable declared inside the class but outside the function, is Instance variable. You can ask if instance variable is declared outside the method then how we will used the instance variable.

Yes, you can use the instance variable by initializing the object.

Let’s understand in detail.

When you will construct the Object at run time, then for that Object the instance variable is initialized. That’s why this is also called Object level variable. If you have not initialized the instance variable, then for each object it is initialized by the default variable of the data type.

For example, default value for int variable is 0,

If you want to access the instance variable then, you need to call through the object instance. Let’s understand the instance variable in detail.

Example:

public class TestingInstance {
   public String portal;
   private String category;
   
   public TestingInstance (String empName) {
      portal = empName;
   }
   
   public void setCategory(String cat) {
	   category = cat;
   }
   
   public void printDetail() {
      System.out.println("Portal  : " + portal );
      System.out.println("Category :" + category);
   }

   public static void main(String args[]) {
      TestingInstance TutDetail = new TestingInstance("TestingLPoint");
      TutDetail.setCategory("Tutorial");
      TutDetail.printDetail();
   }
}

Output:

Portal  : TestingLPoint
Category : Tutorial

In the above example, variable portal and category is the instance variable. The default value is Null. When you assign the value of portal and category through the object reference then the value changed to the parameter you passed. So the value change to TestingLPoint and Tutorial correspondingly.

Let’s see if we want to access the instance variable before object creation, what will happen.

public class TestingInstance {

   public String portal;
   private String category;
   
   public TestingInstance (String empName) {
      portal = empName;
   }
   
   public void setCategory(String cat) {
	   category = cat;
   }
   
   public void printDetail() {
      System.out.println("Poratl  : " + portal );
      System.out.println("Category :" + category);
   }

   public static void main(String args[]) {
	   System.out.println("Poratl  : " + portal );
	   System.out.println("Category :" + category);
      TestingInstance TutDetail = new TestingInstance ("TestingLPoint");
      TutDetail.setCategory("Tutorial");
      TutDetail.printDetail();
   }
}

Output:

Variables in Java

Though main method is static method, it will not be able to access non static instance variable directly. If you will change the Instance variable to static then you can directly access to the instance variable. See below example:

Example:

public class TestingInstance {

   public static String portal;
   private static String category;
   
   public TestingInstance (String empName) {
      portal = empName;
   }
   
   public void setCategory(String cat) {
	   category = cat;
   }
   
   public void printDetail() {
      System.out.println("Poratl  : " + portal );
      System.out.println("Category :" + category);
   }

   public static void main(String args[]) {
	   System.out.println("Poratl  : " + portal );
	   System.out.println("Category :" + category);
      TestingInstance TutDetail = new TestingInstance ("TestingLPoint");
      TutDetail.setCategory("Tutorial");
      TutDetail.printDetail();
   }
}

Output:

Poratl : null
Category :null
Poratl : TestingLPoint
Category :Tutorial

From the above example you can notice that, before object instantiated, the default value “null” is assigned to the instance variable. And we have changed the value by instantiating the object reference.

Static Variable:

Static variable is nothing but the class variable which is instantiate only once at the start of the execution. A single copy of the static variable shared by all instances of class. So that value will be consistent for all instance of class. This can be access directly by the class name.

Syntax:

class_name.variable_name

To declare static variable we need “static” keyword. Addition to the static keyword, this need a data type before the variable name.

Syntax:

Static data_type variable_name;

Example:

class StaticVariableCheck
{
   static int count=0;
   public void variabel_increment()
   {
       count++;
   }
   public static void main(String args[])
   {
	StaticVariableCheck first_increment=new StaticVariableCheck();
    StaticVariableCheck second_increment =new StaticVariableCheck();
    first_increment.variabel_increment();
    second_increment.variabel_increment();
    System.out.println("First increment: count is="+ first_increment.count);
    System.out.println("Second increment: count is="+ second_increment.count);
    third_increment.variabel_increment();
    System.out.println("Third increment: count is="+ third_increment.count);

   }
}

Output:

First increment: count is=2
Second increment: count is=2
Third increment: count is=3

From the above example we have observed that to access the static variable we need a class name.

Can we access the static variable without class name?

Yes, we can access but we need a static method to access static variable.

Let’s see with a simple example.

class StaticWithoutClassName{
  static String name;
  static String purpose;
  //This is a Static Method
  static void describe(){
      System.out.println("Site Name is: "+name);
      System.out.println("Purpose of the site is: "+ purpose);
  }
  // This is also a static method
  public static void main(String args[]) 
  {
	name = "TestingLpoint";
	purpose = "Learning";
       describe();
  }
}

Output:

Site Name is: TestingLpoint
Purpose of the site is: Learning

Now we are clear about the declaration and access the static variable.

Do you know when the static variable is initializing while running the java code?

Let’s discuss on this.

  • When the class loading is complete, the static variable is initializing.
  • Before any object is created, static variable is initialized.   
  • Before any static method is executed, variable is initialized.

Default value for static variable is same as other variable as below.

  • Integer – 0
  • Float : 0.0
  • Boolean : false
  • Object Reference: null

Summary:

  • Class structure in java has mainly 3 components. e.g. Variable, Method & Block
  • There are 3 types of variables in java. e.g. Local variable, Instance variable & Static variable.
  • Variable which is defined inside the function / method is known as Local variable in java.
  • Local variable should be initialized and should not be used outside of the method / function in which this is defined.
  • The variable which is declared inside the class but outside the function, is called Instance variable.
  • If you have not initialized the instance variable, then for each object it is initialized by the default variable of the data type.
  • Static variable is nothing but the class variable which is instantiate only once at the start of the execution. A single copy of the static variable shared by all instances of class.
  • When the class loading is complete, the static variable is initializing.
  • Before any object is created, static variable is initialized.   
  • Before any static method is executed, variable is initialized.

In this section, we have gone through all concept of the class structure in java and variable in java. In next section, we will discuss about the method of the class.

Leave a Reply

Your email address will not be published. Required fields are marked *