当前位置:网站首页>Static keyword
Static keyword
2022-07-27 02:54:00 【l'amour Хэ рутилин】
java Medium static Keyword is mainly used for memory management .
static The purpose of keywords
Easy to call without creating an object ( Method / Variable ). By static Keyword decorated methods or variables do not need to rely on objects for access , As long as the class is loaded , You can access it through the class name .static Can be used to modify member methods of a class 、 Member variables of class , You can also write static Code blocks to optimize program performance .
static Variable
static Variables are also called static variables , The difference between static and non static variables :
Static variables are shared by all objects , There is only one copy in memory , The class is initialized when it is first loaded
Non static variables are owned by objects , Is initialized when the object is created , There are multiple copies , The copies owned by each object do not affect each other
public class Humanity {
static String name;
}
public class Student extends Humanity{
void show(){
System.out.println(" I am a "+name+" Student ");
}
}
public class Teacher extends Humanity{
void show(){
name=" laozhao ";
System.out.println(" I am a "+name+" teacher ");
}
}
public class Test {
public static void main(String[] args) {
Teacher teacher = new Teacher();
teacher.show();
Student student = new Student();
student.show();
}
}

static Method
static Methods also become static methods , Since static methods do not depend on any object, they can be accessed directly , So for static methods , It's not this Of , Because it is not attached to any object , Since there is no object , I can't talk about this 了 , And because of this feature , Non static member variables and non static methods of a class cannot be accessed in a static method , Because non static member variables and non static methods must depend on specific objects to be called .

As you can see from the code above :
Static methods test2() Calling non static member variables age, Compile failed . This is because , No objects were generated at compile time ,age Variables don't exist at all .
Static methods test2() Calling non static method test1(), Compile failed . This is because , The compiler cannot predict methods in non static members test1() Whether non static member variables are accessed in , It is also forbidden to call non static member methods in static methods.
Non static member methods test1() Access static member methods test2()/ Variable name There is no limit
therefore , If you want to call a method without creating an object , You can set this method to static. The most common static method is main Method , That's why main If the method is static, it will be clear at a glance , Because the program is executing main Method, no objects are created , Accessed only by class name .
static block ( Static code block )
Execute as the class loads , And only once
How to write it ,
static{
}
Non static code block :
When executing, if there is a static initialization block , Execute the static initialization block first, and then execute the non static initialization block , It is executed once every object is generated , It can initialize instance variables of a class .
How to write it :
{
}
The execution order of static code blocks : Static code block -----> Non static code block --------> Constructors
Common interview questions
public class Test extends Base{
static{
System.out.println("1");
}
public Test(){
System.out.println("2");
}
public static void main(String[] args) {
new Test();
}
}
class Base{
static{
System.out.println("3");
}
public Base(){
System.out.println("4");
}
}Output results :

You can see that the execution order is Parent static code block ----> Subclass static code block ----> Parent class constructor -----> Subclass construction method
边栏推荐
- How to do the system security test? Let's talk about it in detail
- C language program compilation
- Make static routing accessible to the whole network through ENSP
- Tabbar of customized wechat applet on uni app
- JS utils fragmented
- Rip routing information protocol topology experiment
- OSPF summary (mind map)
- 膜拜,阿里内部都在强推的321页互联网创业核心技术pdf,真的跪了
- "Software testing" packaging resume directly improves the pass rate from these points
- 平成千字文(へいせいせんじもん) (平成12年9月10日 石渡 明 作) 宇宙広遠 銀河永久 日月運行 不乱無休 地球公転 季節変移 黄道星座 太陽年周 故郷群島 南熱北冷 海洋温暖 気候順良 青空飛雲 諸野深緑 湖泉静息 谷川清流 春桜一面 新芽
猜你喜欢

动态设置小程序swiper的高度

从单表到分表实现数据平滑迁移

砺夏行动|源启数字化:既有模式,还是开源创新?

Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

I was fired at the age of 30. I want to understand a few things

time模块: 时间戳、结构化时间、格式化时间的获取与相互转化

Web3.0 world knowledge system sharing - what is Web3.0

无效的目标发行版:17 的解决办法

White box test case design (my grandfather can understand it)
随机推荐
文章摘要智能提取【基于BERT技术】
解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
Static routing experiment configuration
cookie增删改查方法
动态设置小程序swiper的高度
[Li Kou] 1859. Sort sentences
想要彻底搞的性能优化,得先从底层逻辑开始了解~
Getlocation:fail the API need to be declared in the requiredprivateinfo field in app.json
Little sister's notes: how do I learn simple source code to expand my horizons
Basic theoretical knowledge of software testing - concept
JS utils fragmented
【Redis】快速入门
用swiper分类图标
C language program compilation
Towhee 每周模型
Cookie addition, deletion, modification and query methods
小程序utils
Talk about connection pools and threads
js utils 零碎
Three handshakes and four disconnects of TCP