Java nouns and access modifiers, static
1. Package and package reference
(一)package、import
- package:
- Role: To avoid class name conflicts.
- The package name can have a hierarchical structure. The full name of the class : package name. class name .
- Suggestion: All letters of the package name should be lowercase.
- The class names of classes in the same package cannot be the same.
- import:
- Classes in the same package can be accessed directly, but classes in different packages cannot be accessed directly. There are only two ways to access them:
1.1. First, declare the class by import and use the class ------ Suggestion.
1.2. The full name of the class is too cumbersome and not recommended.
Two, access control modifier
(1) Public, private, protected, default
- public: public, any class can be accessed.
- private: private, only accessible in this class.
- protected: protected, this class, derived classes (subclasses), and classes in the same package can be accessed.
- The default (another way of saying it is good): nothing is written, accessible in this class and the same package class.
- Description:
- The access modifier of the class can only be public or the default (not written).
- The access modifiers of the members of the class can be any of the above 4 types.
Code chestnut:
package oop.day05;
//同包类:同一个包下的 class 类文件
//演示访问控制修饰符
public class Aoo {
public int a; //任何类
protected int b; //本类、派生类、同包类
private int d; //本类
int c; //本类、同包类
public void show() {
a = 1;
b = 2;
c = 3;
d = 4;
}
}
class Boo{
//演示private
public void show() {
Aoo o = new Aoo();
o.a = 1;
o.b = 2;
o.c = 3;
//o.d = 4;
}
}
Same package class (same as the class under package oop.day05)
package oop.day05;
public class Coo {
//演示同包类
public void show() {
Aoo o = new Aoo();
o.a = 1;
o.b = 2;
o.c = 3;
//o.d = 4;
}
}
//同包中
class Doo extends Aoo{
//演示protected
public void show() {
a = 1;
b = 2;
c = 3;
// d = 4;
}
}
Different package classes
package oop.day06;
import oop.day05.Aoo;//先导包
//跨包继承
public class Doo extends Aoo{
public void show() {
Aoo aoo = new Aoo();
aoo.a = 1;
aoo.show();
}
}
Three, the role of final keyword modification
- Final means: final and unmodifiable.
- The functions and descriptions of final modified variables, modified methods, and modified classes are as follows:
- Modified variables: Variables cannot be modified.
/* * final修饰成员变量,只能在如下两种方式下初始化: * 1)声明同时初始化 * 2)在构造方法中初始化 * final修饰局部变量,只要在用之前初始化即可 */ //演示final修饰变量 class Eoo{ final int num = 5; final int count; Eoo(){ count = 5; } void show() { final int Number; //count = 8; //编译错误,final修饰的变量不能被改变 } }
- Modification method: The method cannot be overridden.
//演示final修饰方法 class Foo{ final void show() { } void say() { } } class Goo extends Foo{ //void show() {} //编译错误,final修饰的方法不能被重写 void say() { } }
- Modified class: The class cannot be inherited.
//演示final修饰类 final class Hoo{ } //class Ioo extends Hoo{} //编译错误,final的类不能被继承 class Joo{ } final class Koo extends Joo{ }
Four, static
(1) Static variables
- Modified by static.
- Those belonging to the class are stored in the method area and have only one copy.
- It is often accessed through the class name dot.
- When to use: Data shared by all objects.
- Note: Declare and initialize at the same time.
(Two) static method
- Modified by static.
- There is only one copy of those belonging to the class in the method area.
- It is often accessed through the class name dot.
- There is no implicit this transfer in static methods, so instance members cannot be directly accessed in static methods.
- When to use: The operation of the method is only related to the parameters, not the object.
(Three) static block
- Modified by static.
- Belongs to a class, which is automatically executed during the loading of the class; the class is only loaded once, and all static blocks are also executed once.
- When to use: Loading/or initializing static blocks is automatically executed during class loading (executed once).
- Usage scenarios: For example, when opening the program, you need to initialize static resources (pictures, audio, video, etc.); after learning there is a JDBC knowledge content in the later stage, the connection between the project and the database, you need to load the database interface driver first...
Code demo:
//static的演示
public class StaticDemo {
public static void main(String[] args) {
Loo o1 = new Loo();
o1.show();
Loo o2 = new Loo();
o2.show();
Loo o3 = new Loo();
o3.show();
System.out.println("最后的静态变量:"+Loo.b); //常常通过类名点来访问
System.out.println("------------------------------------------");
Moo m = new Moo();
System.out.println(Moo.b);//通过类名点来访问
System.out.println(m.a);
int s = m.test2();
System.out.println(s);
System.out.println("------------------------------------------");
Noo o4 = new Noo();
Noo o5 = new Noo();
Noo o6 = new Noo();
}
}
class Loo{
//演示静态变量
int a;
static int b;
Loo(){
a++;
b++;
}
void show() {
System.out.println("静态变量:"+a+","+b);
}
}
//静态变量,static修饰 属于类的,存储在方法区中,只有一份
class Moo{
//演示静态方法
int a=88;
static int b=56;
void show() {
//有隐式this
System.out.println(this.a);
System.out.println(Moo.b);
}
static void test() {
//没有隐式this
//静态方法没有隐式的this传递
//没有this就意味着没有对象
//而实例变量a必须通过对象点来访问
//所以此处编译错误,因为静态方法中不能直接访问实例成员,得先创建对象再访问。
//System.out.println(a); //编译错误
System.out.println(b);
}
int test2() {
return a;
}
}
class Noo{
static {
System.out.println("静态块");//静态块只加载一次
}
Noo(){
System.out.println("构造方法");
}
}
Result graph:
Five, static final constant
- It must be declared and initialized at the same time before it can be used.
- It is usually accessed through the class name dot and cannot be changed.
- Suggestion: All letters of the constant name should be capitalized, and multiple words should be separated by _ underscore.
- The compiler directly replaces constants with specific numbers during compilation, which is highly efficient.
- When to use: When the data needs to remain unchanged, use it frequently.
//static final常量的演示
public class StaticFinalDemo {
public static void main(String[] args) {
System.out.println(Aoo1.PI); //常量通过类名点来访问
//Aoo.PI = 3.1415926; //编译错误,常量不能被改变
//1)加载Boo.class到方法区中
//2)静态变量num也存储到方法区中
//3)到方法区中获取num的值并输出
System.out.println(Boo.num);
//编译器在编译时将常量直接替换为具体的值,效率高
//相当于System.out.println(5);
System.out.println(Boo.COUNT);
}
}
class Boo{
public static int num = 5; //静态变量
public static final int COUNT = 5; //常量,常量名所有字母都大写
}
class Aoo1{
public static final double PI = 3.14159;
//public static final int NUM; //编译错误,常量必须声明同时初始化
}
Please be patient for the follow-up content and come soon! Writing is not easy, please give a thumbs up 3Q, Thanks!
The chapter list is here: https://blog.csdn.net/qq_41254299/article/details/106638651
Please indicate the source for reprinting: https://blog.csdn.net/qq_41254299
This article is from [Superclover_'s blog]