Understanding of public and static in java Original post: https://blog.csdn.net/v7595v/article/details/45845347

Original post: https://blog.csdn.net/v7595v/article/details/45845347

First is public 

Before explaining these four keywords, I want to make a simple definition of the relationship between classes. For classes that inherit their own , base class can think that they are all their own children, and for classes in the same directory as themselves , consider themselves friends.

 

1. public : public indicates that the data members and member functions are open to all users, and all users can call them directly

 

2. private : private means private , private means that no one can use it directly except the class itself. Private property is sacred and inviolable, even children and friends cannot use it.

 

3. Protected : For children and friends, protected is public and can be used freely without any restrictions, while for other external classes , protected becomes private .

Scope current class same package descendant class other package 

public       √             √                √               √ 

protected √             √                               × 

friendly     √             √                ×               × 

private     √ × × ×                                          

Defaults to friendly when not written



then static

static means static in java

But what does this static mean?

When I first came into contact with static, I didn't understand the concept in the book at all!

I have been in contact with java for a while, and here I want to summarize my understanding of static:

(Concept) static means "global" or "static", which is used to modify member variables and member methods, and can also form static static code blocks, but there is no concept of global variables in the Java language.

This sentence means that you will use static to modify some variables, methods, method blocks, etc. This is where you use static; " However, there is no concept of global variables in java " , how to understand this sentence? You must always remember that java is an object-oriented language, everything is an object, no matter what you do, you need to create an object, and then call the method of this object, how can java allow the existence of a "global variable that is common to the whole world" "The concept of existence? Therefore, there is no so-called "global variable" in java at all, but the absence of this concept does not mean that you cannot implement this function. The purpose of java is to allow people to use it to achieve the effect people want, so static value can be achieved. The role of global variables, so you should now understand the following sentence.

(Concept) Static member variables and member methods modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of the static variable is not generated, but all instances of the class share the same static variable.

Now further understand the following two concepts:

(Concept) As soon as the class is loaded, the Java virtual machine can find them in the method area of ​​the runtime data area based on the class name. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.

To understand this concept based on a practical example, there are now two classes, Demo1 and Demo2.

This instance also contains operations on static methods

public class Demo1 {
    public static int i = 1;
    public int j = 2;
    
    public static int getNumber(){
        return i;
//        这个return返回的是全局变量的i,即前面多创建的i
    }
    
    public int getDealNumber(int j){
        return j;
//        这个return返回的是所传进来的参数,是(int j)这个东西
    }

}


public class Demo2 {
    public static void main(String args[]){
//        想要得到Demo1中的静态的(即全局的)变量i,直接用类名引用就可以了
        int i = Demo1.i;
//        但是想要得到Demo1中的实例的变量j,我需要怎么做呢?(此刻牢记java面向对象的思想!)
//        首先我要先new一个Demo1的对象,然后才可以通过new出来的对象得到Demo1中的j
        Demo1 demo1 = new Demo1();
        int j = demo1.j;
//        同理,java中的static方法和非static方法都是一样的区别
//        下面一行的方法是静态的,我可以直接根据类名调用方法
        int ii = Demo1.getNumber();
//        但是想要调用实例的方法,就需要利用前面所new出来的Demo1的对象来调用了
        int jj = demo1.getDealNumber(1);
//        所以现在你可以理解上文紫色的定义了么?
    }
}

(概念)用public修饰的static成员变量和成员方法本质是全局变量和全局方法,当声明它类的对象市,不生成static变量的副本,而是类的所有实例共享同一个static变量。

弄懂了上面的代码,这句定义是不是迎刃而解了呢?这里面涉及到堆栈的问题,我现在还没有整理,我会整理的,写完这篇我马上就要巩固一下堆栈的知识,我认为堆栈的知识很重要,如果你也是初学者,关注我吧,嘿嘿~


这里说一下静态变量和实例变量的区别:

两者的区别是:
 对于静态变量在内存中只有一个拷贝(节省内存),JVM只为静态分配一次内存,在加载类的过程中完成静态变量的内存分配,可用类名直接访问(方便),当然也可以通过对象来访问(但是这是不推荐的)。
 对于实例变量,没创建一个实例,就会为实例变量分配一次内存,实例变量可以在内存中有多个拷贝,互不影响(灵活)。

所以一般在需要实现以下两个功能时使用静态变量:
在对象之间共享值时
 方便访问变量时

现在理解一下静态方法

2、静态方法
静态方法可以直接通过类名调用,任何的实例也都可以调用静态方法。前面的代码中有直接通过类名调用的例子
静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实例方法(就是不带static的成员变量和成员成员方法),只能访问所属类的静态成员变量和成员方法。因为实例成员与特定的对象关联!就是java面向对象的思想,实例是你这个类本身的属性,你会用这个本身的属性去做一些事情,而这些事情不是固定的,不能像静态方法一样一成不变。换位思考,人是一个java类,手是类的实例方法,而人身上有手机,这个手机就是静态方法;人只会用手去玩手机,你见过人用手机玩手么?哈哈
因为static方法独立于任何实例,因此static方法必须被实现,而不能是抽象的abstract。静态方法是类内部的一类特殊方法,只有在需要时才将对应的方法声明成静态的,一个类内部的方法一般都是非静态的

怎么理解上面这句定义呢?记住得了,哈哈。

3、static代码块

 static代码块也叫静态代码块,是在类中独立于类成员的static语句块,可以有多个,位置可以随便放,它不在任何的方法体内,JVM加载类时会执行这些静态的代码块,如果static代码块有多个,JVM将按照它们在类中出现的先后顺序依次执行它们,每个代码块只会被执行一次。例如:

public class Test5 {
private static int a;
private int b;

static{
Test5.a=3;
System.out.println(a);
Test5 t=new Test5();
t.f();
t.b=1000;
System.out.println(t.b);
}
static{
Test5.a=4;
System.out.println(a);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
}
static{
Test5.a=5;
System.out.println(a);
}
public void f(){
System.out.println("hhahhahah");
}
}  

运行结果:
3
hhahhahah
1000
4
5

 利用静态代码块可以对一些static变量进行赋值,最后再看一眼这些例子,都一个static的main方法,这样JVM在运行main方法的时候可以直接调用而不用创建实例。

还用我再说什么么?

有时你希望定义一个类成员,使它的使用完全独立于该类的任何对象。通常情况下,类成员必须通过它的类的对象访问,但是可以创建这样一个成员,它能够被它自己使用,而不必引用特定的实例。在成员的声明前面加上关键字static(静态的)就能创建这样的成员。如果一个成员被声明为static,它就能够在它的类的任何对象创建之前被访问,而不必引用任何对象。你可以将方法和变量都声明为static。static 成员的最常见的例子是main( ) 。因为在程序开始执行时必须调用main() ,所以它被声明为static。
如果读不懂这句话就倒着念,就明白个大概了。

注意一下几点

声明为static的变量实质上就是全局变量。当声明一个对象时,并不产生static变量的拷贝,而是该类所有的实例变量共用同一个static变量。声明为static的方法有以下几条限制:

它们仅能调用其他的static 方法。

它们只能访问static数据。

它们不能以任何方式引用this 或super(关键字super 与继承有关,在下一章中描述)。
如果你需要通过计算来初始化你的static变量,你可以声明一个static块,Static 块仅在该类被加载时执行一次。

下面的例子显示的类有一个static方法,一些static变量,以及一个static 初始化块:
// Demonstrate static variables,methods,and blocks.

class UseStatic {
static int a = 3;
static int b;

static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

static {
System.out.println("Static block initialized.");
b = a * 4;
}

public static void main(String args[]) {
meth(42);
}
}

一旦UseStatic 类被装载,所有的static语句被运行。首先,a被设置为3,接着static 块执行(打印一条消息),最后,b被初始化为a*4 或12。然后调用main(),main() 调用meth() ,把值42传递给x。3个println ( ) 语句引用两个static变量a和b,以及局部变量x 。

注意:在一个static 方法中引用任何实例变量都是非法的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723354&siteId=291194637