首页 > Java > java教程 > 正文

Java程序用于通过静态方法检查实例变量的可访问性

王林
发布: 2023-09-03 18:21:06
转载
1177 人浏览过

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 belong to the class where it is defined. They can be public, private, protected, or 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. We call them using their class name only because static method belongs to 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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!