ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【Java学习】-the static keyword

2022-02-14 23:03:35  阅读:208  来源: 互联网

标签:Java keyword created object method static particular class


静态主方法只能访问静态变量, 如果要访问动态变量,必须将变量实例化。

在JAVA程序中,除了主静态方法以及类方法之外,其他在该类下定义的实例变量、实例方法,在主类方法中调用时必须要将其实例化,就是要加上对象的引用。

Ordinarily, when you create a class you are describing how objects of that class look and how they will behave.

You don't actually get an object until you create one using new, and at that point, storage is allocated and methods become available.

There are two situations in which this approach is not sufficient.

One is if you want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created,

or even if no objects are created.

The other is if you need a method that isn't associated with any particular object of this class. That is, you need a method that you can 

call even if no objects are created.

You can achieve both of these effects with the static keyword.

When you say something is static, it means that particular field or method is not tied to any particular object instance of that class.

So even if you've never created an object of that class you can call a static method or access a static field.

With ordinary, non-static fields and methods, you must create an object and use that object to access the field or method, since

non-static fields and methods must know the particular object they are working with.

Using the class name is the preferred way to refer to a static variable.

 

标签:Java,keyword,created,object,method,static,particular,class
来源: https://www.cnblogs.com/yidianling/p/15894532.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有