总结
1、
modifier 改性剂 修饰符 修饰语 调节器
access modifier 访问修饰符 存取权限 存取修饰词 存取修饰字
官网“can”
Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:
是否允许
2、
2个方面
类的存取修饰词
方法的存取修饰词
3、
默认可见性
类的默认存取修饰符是限制在包package内、包内可见;
方法也是;
可见性有4种
public/protected/no modifier/private
4、
子类已经是另外一个包了,是包外了
The third column indicates whether subclasses of the class declared outside this package have access to the member.;
5、
除了常量constants外,避免public;
使用public限制了修改代码的自由度;
6、
public class Bicycle {

private int cadence;
private int gear;
private int speed;

// add an instance variable for the object ID
private int id;

// add a class variable for the
// number of Bicycle objects instantiated
private static int numberOfBicycles = 0;
...
}
Class Variables两种访问方式
Bicycle.numberOfBicycles
myBike.numberOfBicycles
Class Methods两种访问方式
ClassName.methodName(args)
instanceName.methodName(args)

7、
怎样生成自行车的id和计量自行车的数量
public class Bicycle {

private int cadence;
private int gear;
private int speed;
private int id;
private static int numberOfBicycles = 0;

public Bicycle(int startCadence, int startSpeed, int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed;

// increment number of Bicycles
// and assign ID number
id = ++numberOfBicycles;
}

// new method to return the ID instance variable
public int getID() {
return id;
}
...
}

8、
Not all combinations of instance and class variables and methods are allowed:

Instance methods can access instance variables and instance methods directly.
Instance methods can access class variables and class methods directly.
Class methods can access class variables and class methods directly.
Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

实例方法可以直接访问实例方法、实例变量;也可以直接访问类方法、类变量;
类方法可以直接访问类变量

9、
static final经常用来定义常量constants
static final double PI = 3.141592653589793;

 
 
2016年08月01日 23:02:46 S1amDuncan 阅读数:6869
 
Java成员变量、局部变量、静态变量、成员方法、全局方法等概念的区别 - S1amDuncan的博客 - CSDN博客 https://blog.csdn.net/s1amduncan/article/details/52089944

在Java中,一个类体由2部分构成:
一部分是变量的定义;
一部分是方法的定义(一个类中可以有多个方法)

Java中的变量可以分为成员变量,全局变量

成员变量和局部变量的区别

成员变量:(类似于C中的全局变量的概念,所以也可以说是全局变量)

①成员变量定义在类中,在整个类中都可以被访问。

②成员变量随着对象的建立而建立,随着对象的消失而消失,存在于对象所在的堆内存中。

③成员变量有默认初始化值。

局部变量:

①局部变量只定义在局部范围内,如:函数内,语句内等,只在所属的区域有效。

②局部变量存在于栈内存中,作用的范围结束,变量空间会自动释放。

③局部变量没有默认初始化值

在使用变量时需要遵循的原则为:就近原则

首先在局部范围找,有就使用;接着在成员位置找。

静态变量(也叫做类变量,类属性)

由static修饰的变量称为静态变量,其实质上就是一个全局变量。如果某个内容是被所有对象所共享,那么该内容就应该用静态修饰;没有被静态修饰的内容,其实是属于对象的特殊描述。

成员变量和静态变量的区别

1、两个变量的生命周期不同

成员变量随着对象的创建而存在,随着对象被回收而释放。

静态变量随着类的加载而存在,随着类的消失而消失。

2、调用方式不同

成员变量只能被对象调用。

静态变量可以被对象调用,还可以被类名调用。

3、别名不同

成员变量也称为实例变量。

静态变量也称为类变量。

4、数据存储位置不同

成员变量存储在堆内存的对象中,所以也叫对象的特有数据。

静态变量数据存储在方法区(共享数据区)的静态区,所以也叫对象的共享数据。

列表对比:

成员变量、局部变量、静态变量的区别

成员变量

局部变量

静态变量

定义位置

在类中,方法外

方法中,或者方法的形式参数

在类中,方法外

初始化值

有默认初始化值

无,先定义,赋值后才能使用

有默认初始化值

调用方式

对象调用

---

对象调用,类名调用

存储位置

堆中

栈中

方法区

生命周期

与对象共存亡

与方法共存亡

与类共存亡

别名

实例变量

---

类变量

  1.  
    class Demo{
  2.  
    int x;// 非静态成员变量,又称为属性,对该类不同的对象来说,属性是不同的
  3.  
    static int y;// 静态成员变量,一个类中只有一个该变量,该类不同的对象共享同一个静态成员变量
  4.  
    public static void main(String[] args){
  5.  
    int m = 0;// 局部变量,是方法内部定义的变量,只在方法内部可见,在该方法结束后,由垃圾回收器自动回收
  6.  
    }
  7.  
    }

Java中的方法可以分为成员方法,全局方法,构造方法

  1.  
    public class Test {
  2.  
    private int age; //这是成员变量
  3.  
    public Test(int age) { //这是构造方法
  4.  
    this.age = age;
  5.  
    }
  6.  
    public void setAge(int age) { //这是成员方法
  7.  
    this.age = age;
  8.  
    }
  9.  
    public static int getAge() { //这是全局方法,加了static关键字,成员方法就变成了全局方法
  10.  
    return this.age;
  11.  
    }
  12.  
    }

成员方法必须用类的实例化对象进行访问,而全局方法是直接可以用类名.方法名来访问的,构造方法则是实例化对象时进行初始化的

 

Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects) https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

  • At the top level—public, or package-private (no explicit modifier).
  • At the member level—publicprivateprotected, or package-private (no explicit modifier).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

The following table shows the access to members permitted by each modifier.

Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.

Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.

Classes and Packages of the Example Used to Illustrate Access Levels

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.

The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.

Visibility
Modifier Alpha Beta Alphasub Gamma
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N

Tips on Choosing an Access Level:

If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.

  • Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
  • Avoid public fields except for constants. (Many of the examples in the tutorial use public fields. This may help to illustrate some points concisely, but is not recommended for production code.) Public fields tend to link you to a particular implementation and limit your flexibility in changing your code.

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases.

Understanding Class Members

In this section, we discuss the use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class.

Class Variables

When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadencegear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. For this you need a class variable, numberOfBicycles, as follows:

public class Bicycle {

    private int cadence;
private int gear;
private int speed; // add an instance variable for the object ID
private int id; // add a class variable for the
// number of Bicycle objects instantiated
private static int numberOfBicycles = 0;
...
}

Class variables are referenced by the class name itself, as in

Bicycle.numberOfBicycles

This makes it clear that they are class variables.


Note: You can also refer to static fields with an object reference like

myBike.numberOfBicycles

but this is discouraged because it does not make it clear that they are class variables.


You can use the Bicycle constructor to set the id instance variable and increment the numberOfBicycles class variable:

public class Bicycle {

    private int cadence;
private int gear;
private int speed;
private int id;
private static int numberOfBicycles = 0; public Bicycle(int startCadence, int startSpeed, int startGear){
gear = startGear;
cadence = startCadence;
speed = startSpeed; // increment number of Bicycles
// and assign ID number
id = ++numberOfBicycles;
} // new method to return the ID instance variable
public int getID() {
return id;
}
...
}

Class Methods

The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args)

Note: You can also refer to static methods with an object reference like

instanceName.methodName(args)

but this is discouraged because it does not make it clear that they are class methods.


A common use for static methods is to access static fields. For example, we could add a static method to the Bicycle class to access the numberOfBicycles static field:

public static int getNumberOfBicycles() {
return numberOfBicycles;
}

Not all combinations of instance and class variables and methods are allowed:

  • Instance methods can access instance variables and instance methods directly.
  • Instance methods can access class variables and class methods directly.
  • Class methods can access class variables and class methods directly.
  • Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.

Constants

The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.

For example, the following variable declaration defines a constant named PI, whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter):

static final double PI = 3.141592653589793;

Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so. By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated by an underscore (_).


Note: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time constant. If the value of the constant in the outside world changes (for example, if it is legislated that pi actually should be 3.975), you will need to recompile any classes that use this constant to get the current value.

The Bicycle Class

After all the modifications made in this section, the Bicycle class is now:

public class Bicycle {

    private int cadence;
private int gear;
private int speed; private int id; private static int numberOfBicycles = 0; public Bicycle(int startCadence,
int startSpeed,
int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed; id = ++numberOfBicycles;
} public int getID() {
return id;
} public static int getNumberOfBicycles() {
return numberOfBicycles;
} public int getCadence() {
return cadence;
} public void setCadence(int newValue) {
cadence = newValue;
} public int getGear(){
return gear;
} public void setGear(int newValue) {
gear = newValue;
} public int getSpeed() {
return speed;
} public void applyBrake(int decrement) {
speed -= decrement;
} public void speedUp(int increment) {
speed += increment;
}
}
 
 
 
 

The third column indicates whether subclasses of the class declared outside this package have access to the member.;的更多相关文章

  1. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL/SQL Statement ignored 问题

    前段时间由于修改SMES系统,出现了一个问题. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL ...

  2. Summary: difference between public, default, protected, and private key words

    According to Java Tutorial: Controlling Access to Members of a Class Access level modifiers determin ...

  3. Controlling Access in Java

    Referrence: Oracle Java Doc Two levels top level: public, or package-private (no explicit modifier) ...

  4. PHP执行insert语句报错“Data too long for column”解决办法

    PHP执行mysql 插入语句, insert语句在Navicat for mysql(或任意的mysql管理工具) 中可以正确执行,但是用mysql_query()函数执行却报错. 错误 : “Da ...

  5. Android Lint Checks

    Android Lint Checks Here are the current list of checks that lint performs as of Android Studio 2.3 ...

  6. Hibernatel框架关联映射

    Hibernatel框架关联映射 Hibernate程序执行流程: 1.集合映射 需求:网络购物时,用户购买商品,填写地址 每个用户会有不确定的地址数目,或者只有一个或者有很多.这个时候不能把每条地址 ...

  7. Fedora 22中的DNF软件包管理工具

    Introduction DNF is the The Fedora Project package manager that is able to query for information abo ...

  8. HBase框架学习之路

    1 背景知识 1.1 解决问题 解决HDFS不支持单条记录的快速查找和更新的问题. 1.2 适用情况 存在亿万条记录的数据库,只有千万或者百万条记录使用RDBMS更加合适 确保你的应用不需要使用RDB ...

  9. SSH整合

    首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

随机推荐

  1. webApi之FromUri和FromBody区别

    public Link GetLink([FromUri] FileRequest fileRequest) { if (ModelState.IsValid) { var xml = WebConf ...

  2. 3. Oracle数据库逻辑备份与恢复

    一. Oracle逻辑备份介绍 Oracle逻辑备份的核心就是复制数据:Oracle提供的逻辑备份与恢复的命令有exp/imp,expdp/impdp.当然像表级复制(create table tab ...

  3. scala中Map和Tuple

    /** * Created by root * Description : Tuple and Map */ object MapTest { def main(args: Array[String] ...

  4. window下JBoss7 安装部署

    0x01 下载安装 1.下载地址: http://www.jboss.org/jbossas/downloads 2.解压缩:选择一个安装目录解压 jboss-as-7.1.1.Final.zip 3 ...

  5. [NodeJS] Node.js 与 V8 的故事

    要说Node.js的历史,就不得不说说V8历史.在此之前我们先一句话描述一下什么是Node.js:Node.js是一个基于Google Chrome V8 Javascript引擎之上的平台,用以创建 ...

  6. [Command] lrzsz - 文件传输工具包

    lrzsz 是一个支持 XMODEM.YMODEM.ZMODEM 文件传输协议的 Unix 程序包.它是 Omen Technologies 公司所有的 rzsz 程序包的公开发行增强版,遵守 GNU ...

  7. Mac上zip,rar,tar文件命令解压和压缩

    经常遇到在windowns上的压缩文件,在mac上解压出现问题,特意总结了下在Terminal里常用命令的方式解压和压缩文件 1.zip压缩文件 zip命令的参数很多,可以利用"zip -- ...

  8. LINUX安装中文输入法和那些大坑

    明明有很多事要做,却偏偏不知道要做什么,这种感觉,很令人上火. 一.基础知识 在原生ubuntu14.04英文环境系统中只有IBus拼音,真的好难用.由于搜狗输入法确实比Linux系统下其它的中文输入 ...

  9. WINDOWS消息和窗口简介

    一.WINDOWS的消息和窗口简介:1.什么是windows在这里我就不介绍了,但是作为一个程序员我们要知道WINDOWS最重要的一个也是我们程序员常用的一个东西就是消息.窗口是以消息的形式输入的,窗 ...

  10. 使用es6的蹦床函数解决递归造成的堆栈溢出

      首先,我们先定义一个函数,使用递归的思想写求和的方法: function sum(x, y) { if (y > 0) { return sum(x + 1, y - 1); } else ...