non-static members and its control flow in Java

Reading Time: 3 minutes

Class level members which don’t have static keyword in it’s creation statement are known as Non-static members, like non-static variable and non-static methods.

Type of non-static members

  1. Non static variables
  2. Non-static blocks
  3. Non-static block
  4. Constructors

non-static variables

  1. Non-static variables are class level variables that do not have static keyword in its definition.
  2. When we create object, inside that object the non-static variables get memory and assigned with default variable if there is no assignment. example giving below
public class Student {

    private String name;
    private int age;
    private String bloodGroup = "O+";

    public static void main(String[] args) {
        Student student = new Student();
        // student.name variable is null, because String is a class and default is null.
        System.out.println("name : " + student.name);
        // student.age variable is 0, because int is primitive data-type and its default value is 0. 
        System.out.println("age : " + student.age);
        // student.bloodGroup variable initially assigned with default value null, but after that we have assignment for that so finally it will be "O+".
        System.out.println("blood group : " + student.bloodGroup);
    }
}
OUTPUT
name : null
age : 0
blood group : O+

non-static block

  1. A non-static blocks are class level blocks which do not have any prototype.
  2. The main reason why we need a non-static block is to execute any logic whenever an object is create irrespective of the constructor.
  3. The JVM automatically calls the Non static blocks whenever an object gets create in the Java stack region.
  4. The order of execution of non-static blocks is an order as they are define.
class Student {

    Student(){
        System.out.println("Constructor");
    }

    // non-static block
    {
        System.out.println("Non-static block");
    }

    public static void main(String[] args) {
        Student student = new Student();

    }
}
OUTPUT
Non-static block
Constructor

Non-static method

  1. Non-static methods are those that do not have the static keyword before the method name.
  2. Like non-static variables, non-static methods also should be call only using object.
  3. Without an instance of the class, we can’t access non-static variables inside a static method. But both static and non-static variables can be access in non-static method, this is possible because of this keyword in non-static method which represent for current instance.
public class Student {

    public static void main(String[] args) {

        // if we try to call non-static method without using instance from static method it will get error || Non-static method 'getName()' cannot be referenced from a static context
        // getName();
        Student student = new Student();
        student.greet("Knoldus");
    }
    public void greet(String name){
        System.out.println("Hello  " + name);
    }
}
OUTPUT
Hello  Knoldus

Constructor

  1. Constructors are special member functions whose task is to initialize the objects of its class.
  2. It is treated as a special member function because its name is the same as the class name.
  3. Java constructors are invoked when their objects are created. It is named such because, it constructs the value, i.e., provide data for the object, i.e., they are used to initialize objects.
  4. Every class has a constructor when we don’t explicitly declare a constructor for any java class the compiler creates a default constructor for that class which does not have any return type.
  5. The constructor in Java cannot be abstract, static, final or synchronize and these modifiers are not allow for the constructor.

Control flow of non-static members

OUTPUT

  1. Firstly, we don’t have any static variable or static block in code, so Jvm start execution from main method.
  2. In main method we are creating object of CoachingStudent class.
  3. Once the object is create using new keyword.
    1. Object is create with default values based on the primitive data type or if data-type is of class type then null is default value.
    2. Now, in this point the values of variable is
      1. name = null, id = 0, currentCity = null, bloodGroup = null
    1. Now, after creating memory with default values JVM will check if any initialization statement is available, so
      1. Now JVM will assign value Mumbai to currentCity.
      2. name = null, id = 0, currentCity = Mumbai, bloodGroup = null
    2. Now Jvm executes the non-static block, inside non-static block we initilizing the value O+ to bloodGroup.
      1. name = null, id = 0, currentCity = Mumbai, bloodGroup = O+
      2. this keyword is represnt for current object/instance.
    3. After these steps JVM executes the constructor of the class.
    4. Lastly when we call non-static method using object/instance, JVM will execute then.

For JVM- Runtime Data Area :
https://blog.knoldus.com/full-explanation-of-jvm-runtime-data-area-and-how-jvm-using-it/
For JVM Architecture : 
https://blog.knoldus.com/how-the-new-jvm-works-and-its-architecture/


Knoldus-blog-footer-image

Written by 

I'm having 2.5+ years of experience in Java technologies. I have worked on Java SE, Java EE, Spring, Hibernate, Kafka, Apache Beam, SQL, Scala, etc. I am curious about learning new technologies.