首頁 > Java > java教程 > Java程式用於透過靜態方法檢查實例變數的可訪問性

Java程式用於透過靜態方法檢查實例變數的可訪問性

王林
發布: 2023-09-03 18:21:06
轉載
1245 人瀏覽過

Java程式用於透過靜態方法檢查實例變數的可訪問性

靜態方法使用靜態關鍵字定義,但是聲明實例變數時不使用靜態關鍵字。通常情況下,我們不能透過靜態方法存取實例變數。

在本文中,我們將建立一個實例變量,然後透過一個靜態方法來檢查該實例變數的可訪問性。首先讓我們來了解靜態方法和實例變數。

Instance Variable

在程式語言的上下文中,變數是包含任何類型資料的容器的名稱。我們可以說它是一個儲存單元。

Syntax to declare a variable

Data_Type nameOfvariable = values [optional];
登入後複製

A variable can be initialized at the time of declaration or we can initialize it when required in the whole program. But value must be of the same data type as specified at the time of declaration.

實例

String str; 
// only declaration
int price = 59; 
// declaration and initialization
str = “Tutorix”; 
// initialization of ‘str’  
登入後複製

Instance variables are one of the types of variables that are non-static. They are declared inside class but outside of every method scope of that class. They are accessible to all methods that thatong to class class. can be public, private, protected, 或 default. If we don't mention any access specifier then variable will be initialized with default access specifier.

Static Method

Static methods are defined using non access modifier static keyword. Normally, we call a method by creating an object of its class but static methods can be called without using objects。 class not to the instance of that class.

To call Static Mehtod

Class_name.static_method_name
登入後複製

For example, most of the members of inbuilt class Math is static. We can use them directly without creating its object.

Example 2

public class Main {
   public static void main( String[] args ) {
      double x = 6.55;
      double y = 4.32;
      System.out.println(" Ceil value of x: " + Math.ceil(x) );
      System.out.println(" Floor value of y: " + Math.floor(y) );
      System.out.println(" Value of PI = " + Math.PI);
   }
}
登入後複製

輸出

Ceil value of x: 7.0
Floor value of y: 4.0
Value of PI = 3.141592653589793
登入後複製

上面的範例說明了Math類別的靜態方法ceil()和floor()的使用。我們可以看到,在我們的程式中直接使用了它們,而不需要建立任何Math類別的物件。

透過靜態方法檢查實例變數的可訪問性

We already mentioned earlier in this article that we can’t access instance variables by a static method directly, we can only access them by creating an instance or object of the class.

##Example

The following program demonstrates whether we can access instance variable by static method main() or not.

public class Main {
   public String str = "Tutorialspoint"; 
   public static void main(String []args) {
      System.out.println(" Accessing instance variable " + str);
   }
}
登入後複製

If we try to run the above code we will get an error.

輸出

Main.java:4: error: non-static variable str cannot be referenced from a static context
   System.out.println(" Accessing instance variable " + str);
                                                        ^
1 error
登入後複製

Example

下面的程式範例說明如何透過靜態方法存取實例變數。我們將創建一個Main類別的物件'obj',透過使用這個對象,我們可以存取變數'str'。

public class Main {
   public String str = "Tutorialspoint"; 
   public static void main(String []args) {
      Main obj = new Main(); 
      // creating object using new keyword
      // To access the instance variable ‘str’
      System.out.println(" Accessing instance variable: " + obj.str);
   }
}
登入後複製

輸出

Accessing instance variable: Tutorialspoint
登入後複製
Conclusion

在本文中,我們了解了實例變數和靜態方法的概念。此外,我們也透過Java程式討論了靜態方法對實例變數的可存取性。

以上是Java程式用於透過靜態方法檢查實例變數的可訪問性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板