Java - Calling Non Static Members Directly From Constructor Without Using the Object Name Last Updated : 24 Sep, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Java is an Object-Oriented Programming(OOP) language. We need to create objects in order to access methods and variables inside a class. However, this is not always true. While discussing static keywords in java, we learned that static members are class level and can be accessed directly without any instance. Here we will be discussing how we can access non-static data members without using the object name for which let us compare static data members and non-static data members which are as provided in the below table as follows: Static Data MembersNon-Static Data MembersThey are declared using the keyword 'static'.Every member in java defaults to a non-static without a 'static' keyword preceding it.All objects of a class share the same copy of static data members.Each object of the class gets its own copy of Non-Static data members.They can be accessed using the class name or object.They are generally accessed through an object of the class.Static methods can be called without the instance or object of that class. Non-static methods can access any static method and static variable, without creating an instance of the object. Example 1: Calling static data members without the instance or object of that class. Java // Java program Illustrating Calling Static Data Members // Without the Instance or object of that class // Main class public class GFG { // Static variable static int a = 5; // Method 1 // Static method static void f() { // Print statement whenever static method is called System.out.println("I am static method"); } // Method 2 // Main driver method public static void main(String[] args) { // Calling static data members without the // instance or object of that class. System.out.println(GFG.a); // Calling method 1 GFG.f(); } } Output5 I am static method Example 2: Calling non-static data members. Java // Java Program Illustrating Calling Non-static Data Members // Main class public class GFG { // Non-static variable int a = 5; // Method 1 // Non-static method void f() { // Print statement whenever non static method is // called System.out.println("I am Non-static method"); } // Method 2 // Main driver method public static void main(String[] args) { // Creating object of class inside main() GFG obj = new GFG(); System.out.println(obj.a); // Calling non static data members obj.f(); } } Output5 I am Non-static method Example 3: Calling non-static data members directly without using object names. Java // Java program Illustrating Calling Non-static Method // Without using Object Name // Main class class GFG { // Calling non-static method // through constructor GFG() { int a = 5; System.out.println(a); display(); } // Method 1 // Non static method void display() { // Print statement whenever non-static method is // called System.out.println( "Non static method is called using constructor."); } // Method 2 // Main driver method public static void main(String[] a) { new GFG(); } } Output5 Non static method is called using constructor. Comment More infoAdvertise with us Next Article Java - Calling Non Static Members Directly From Constructor Without Using the Object Name D dattabikash505 Follow Improve Article Tags : Java Blogathon Blogathon-2021 Java-Constructors Practice Tags : Java Similar Reads Constructor Overloading with Static Block in Java In Java, Constructor is a block of codes similar to the method that is used to initialize the objectâs state. A constructor is invoked at the time of object or instance creation. Each time an object is created using a new() keyword at least one constructor (it could be default constructor) is invoke 4 min read Constructor newInstance() method in Java with Examples The newInstance() method of a Constructor class is used to create and initialize a new instance of this constructor, with the initialization parameters passed as parameter to this method. Each parameter is unwrapped to match primitive formal parameters, and both primitive and reference parameters ar 3 min read Constructor toString() method in Java with Examples The toString() method of java.lang.reflect.Constructor class is used to return string representation of this Constructor.This string is the collection of all the properties of this constructor. For creating this string method adds access modifiers of the constructor with the name of the declaring cl 2 min read Constructor toGenericString() method in Java with Examples The toGenericString() method of java.lang.reflect.Constructor class is used to return get the generic form of this constructor, i.e. a string representing the details about this constructor including the type parameters. Syntax: public String toGenericString() Parameters: This method accepts nothing 2 min read Why a Constructor can not be final, static or abstract in Java? Prerequisite: Inheritance in Java Constructor in java is a special type of method which is different from normal java methods/ordinary methods. Constructors are used to initialize an object. Automatically a constructor is called when an object of a class is created. It is syntactically similar to a 6 min read Like