📜  Java - 直接从构造函数调用非静态成员而不使用对象名称

📅  最后修改于: 2022-05-13 01:55:20.875000             🧑  作者: Mango

Java - 直接从构造函数调用非静态成员而不使用对象名称

Java是一种面向对象的编程 (OOP) 语言。我们需要创建对象才能访问类中的方法和变量。然而,这并不总是正确的。在讨论Java的static 关键字时,我们了解到静态成员是类级别的,无需任何实例即可直接访问。在这里,我们将讨论如何在不使用对象名称的情况下访问非静态数据成员,让我们比较下表中提供的静态数据成员和非静态数据成员,如下所示:

Static Data MembersNon-Static Data Members
They 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.

示例 1:在没有该类的实例或对象的情况下调用静态数据成员。

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();
    }
}


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();
    }
}


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(); }
}



输出
5
I am static method



示例 2:调用非静态数据成员。

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();
    }
}


输出
5
I am Non-static method

示例 3:不使用对象名称直接调用非静态数据成员。

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(); }
}


输出
5
Non static method is called using constructor.