Non static members in Java

Previous
Next

Non Static Members:

  • The common functionality of application must be defined as static.
  • Non static represents the specific functionality of Object in application.
  • We can define 4 non static members in a class.
    • Constructor
    • Non static block
    • Non static variable
    • Non static method
  • We must access the functionality using Object address.

Where we create object?

  • Every application execution starts with static context.
  • We create object in static context to enter into non static context.

Create object in main():

  • We create object with new keyword.
  • ‘new’ keyword allocate object memory at random address.
  • We must call constructor in the process of Object creation.

Constructor:

  • It is special java method.
  • It must be defined with Class name.
  • It is used to assign values to non static variable in object creation process.
  • It must be called in object creation process.
public class Pro
{
    Pro()
    {
        System.out.println("Constructor");
    }
    public static void main(String []args)
    {
        new Pro();
    }
}

Constructor executes every time when we create the Object.

public class Pro
{
    Pro()
    {
        System.out.println("Constructor");
    }
    public static void main(String []args)
    {
        for(int i=1 ; i<=10 ; i++)
        {
            new Pro();
        }
    }
}
Previous
Next

Add Comment

Courses Enquiry Form