总结
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. iOS开发-- 如何让 UITableView 的 headerView跟随 cell一起滚动

    在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特. 1. 所以我就把它 作为一个section的 headerView. 也就 ...

  2. openjdk源码阅读

    http://rednaxelafx.iteye.com/blog/1549577 http://blog.csdn.net/fancyerii/article/details/7007503 ├—a ...

  3. CentOS7--配置时间和日期

    CentOS7提供三个命令行工具,可用于配置和显示有关系统日期和时间的信息. timedatectl:Linux 7中的新增功能,也是systemd其中的一部分. date:系统时钟,也成为软件时钟, ...

  4. 流程控制与数组——Java疯狂讲义

    顺序结构 if分支语句      if{} 可以有多个else if{} else{} 可以省略 switch分支语句 while循环 do while循环 for循环 嵌套循环 控制循环结构 理解数 ...

  5. linux C 调用shell程序执行

    #include<stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h ...

  6. linux系统环境搭建

    一.安装jdk 参考帖子 用yum安装JDK(CentOS) 1.查看yum库中都有哪些jdk版本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [r ...

  7. it 删除远程分支

      一不小心把本地的临时分支push到server上去了,想要删除.一开始用git branch -r -d origin/branch-name不成功,发现只是删除的本地对该远程分支的track,正 ...

  8. Linux学习(二)

    Linux进程管理 每个 Linux 命令都与系统中的程序对应,输入命令,Linux 就会创建一个新的进程.例如使用 ls 命令遍历目录中的文件时,就创建了一个进程.简而言之,进程就是程序的实例. 创 ...

  9. sencha touch list(列表) item(单行)单击事件触发顺序

    测试代码如下 Ext.define('app.view.new.List', { alternateClassName: 'newList', extend: 'app.view.util.MyLis ...

  10. rsyslog local0-local7的用法

    很多时候我们需要将一个服务的日志文件导向一个指定的文件,这个时候可以设置log-facility 如在dhcpd.conf中配置 1 : update log-facility in the dhcp ...