0

0

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

王林

王林

发布时间:2023-09-03 18:21:06

|

1344人浏览过

|

来源于tutorialspoint

转载

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.

立即学习Java免费学习笔记(深入)”;

实例

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.

HeyGen
HeyGen

HeyGen是一个AI虚拟数字人生成平台,可以根据用户提供的内容,快速生成高质量的虚拟发言人视频,支持数字化身、文本转视频和视频翻译。

下载

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速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

3

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

23

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

7

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

30

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

2

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

8

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

3

2026.01.15

php图片上传教程汇总
php图片上传教程汇总

本专题整合了php图片上传相关教程,阅读专题下面的文章了解更多详细教程。

2

2026.01.15

phpstorm相关教程大全
phpstorm相关教程大全

本专题整合了phpstorm相关教程汇总,阅读专题下面的文章了解更多详细内容。

4

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 3.8万人学习

TypeScript 教程
TypeScript 教程

共19课时 | 2.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号