Static and final can modify objects and their characteristics

1. Final can modify: class variable, member variable, local variable, method and class.

1.1. Variable modified by final: it must be assigned directly, and it is no longer allowed to change the value elsewhere.

1.2. Method modified by final: cannot be copied.

1.3. Class modified by final: cannot be inherited.

2. Static can modify: member variables and methods.

2.1. Member variables modified by static: member variables become class variables and can be accessed in all methods. Can be reassigned multiple times.

2.2. Methods modified by static: they can be called directly in static methods. It can be called directly in non static methods and static methods. Cannot be replicated by subclasses.

3. Example code:

}
//class Hee extends Pee{ //不能从final类继承
//
//}

public class TempTest {
//final可以是局部变量,成员变量,类变量的修饰符.~对应变量值不可以改.
//satic可以修饰成员变量,方法,但不能修饰局部变量.~~并且静态变量值可以多次被赋值.~~静态变量在静态方法,普通方法里面都可以使用.

//<a href="https://www.jb51.cc/tag/leibianliang/" target="_blank" class="keywords">类变量</a>
static int a=10;
//成员变量
final  int b=11;
//成员变量
final static int c=12;

/**
 * 静态<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>
 */
static void test1(){
    a=20;
    //static int d=1;  //static修饰符不能<a href="https://www.jb51.cc/tag/zaizheli/" target="_blank" class="keywords">在这里</a>使用
    //b=12;            //不能给final赋值
    final int e=2;//局部变量
    //e=20;      //不能给final赋值
}

/**
 * <a href="https://www.jb51.cc/tag/putongfangfa/" target="_blank" class="keywords">普通方法</a>
 */
 void test2(){
    a=30;
    final int c=0;
    test1();
}



public static void main(String[] args){
    test1();
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(a);
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(c);

    TempTest test=new Temp<a href="https://www.jb51.cc/tag/test/" target="_blank" class="keywords">test()</a>;
    Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(test.b);
}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>