当前位置:网站首页>Differences and examples between internal classes and static internal classes
Differences and examples between internal classes and static internal classes
2022-07-27 05:23:00 【Master yuan】
List of articles
1. The difference between inner class and static inner class
Static inner class is independent of outer class , Variables in external classes cannot be accessed directly in static inner classes 、 Method . If you want to visit , It has to be new An object of an external class , Use new Come out of the object to access . But you can access static variables directly , Call static methods .
A normal inner class exists as a member of an outer class , In a normal inner class, you can directly access the properties of the outer class , Call methods of external classes .
If the outer class wants to access the properties of the inner class or call the methods of the inner class , You have to create an internal class object , Use this object to access properties or call methods .
If other classes want to access the properties of ordinary inner classes or call the methods of ordinary inner classes , You must create an object of the inner class in the outer class as a property , External classes can call methods of ordinary internal classes or access attributes of ordinary internal classes through attributes .
If other classes want to access the properties of static inner classes or call the methods of static inner classes , Just create a static inner class object .
2. Illustrate with examples
2.1 Inner class
public class Father {
private String name;
public Father(String name) {
this.name = name;
}
public void say(String dogName) {
Dog dog = new Dog(dogName);
System.out.println("i am " + name + ", i have a dog " + dog.dogName);
}
public void feed() {
System.out.println("feed in Father");
}
public class Dog {
private String dogName;
public Dog(String dogName) {
this.dogName = dogName;
}
public void bark() {
feed();
System.out.println("i am " + dogName + ", i am belong to " + name);
}
public String getDogName() {
return dogName;
}
}
}
For the first 2 strip , Ordinary inner classes as members , You can directly access variables of external classes , Call its methods . You can see Dog Inside bark() Methods can use variables directly name, You can call feed Method .
For the first 3 strip , We are Father Class , Want to use Dog Properties in a class dogName, You must first instantiate a dog object , Then call the method through this object , Get variable .
For the first 4 strip , First create an external class
public class YYTest {
public static void main(String[] args) {
final Father zhangsan = new Father("zhangsan");
Father.Dog gg = zhangsan.new Dog("GG");
gg.getDogName();
gg.bark();
}
}
You can see , Through object zhangsan.new Dog() Method instantiates an internal class object gg, Variables can be accessed through this internal class object , Calling method .
2.2 Static inner class
public class Father {
private String name;
private static String sex = "female";
public Father(String name) {
this.name = name;
}
public void say(String dogName) {
Dog dog = new Dog(dogName);
System.out.println("i am " + name + ", i have a dog " + dog.dogName);
}
public void feed() {
System.out.println("feed in Father");
}
public static class Dog {
private String dogName;
public Dog(String dogName) {
this.dogName = dogName;
}
public void bark() {
Father lisi = new Father("lisi");
lisi.feed();
System.out.println("i am " + dogName + ", i am belong to " + lisi.name + ", his sex is " + sex);
}
public String getDogName() {
return dogName;
}
}
}
For the first 1 spot , You can see bark() Method you want to use name attribute , You need to instantiate Father object , Call... By object . Static variables do not need to be accessed by instantiating objects , You can directly access .
For the first 1 spot
public class YYTest {
public static void main(String[] args) {
Father.Dog hh = new Father.Dog("HH");
hh.bark();
}
}
Just instantiate the static inner class directly . Pay attention to the instantiation method .
Static classes can only be internal .
边栏推荐
- B1024 科学计数法
- mq设置过期时间、优先级、死信队列、延迟队列
- redis集群
- JVM上篇:内存与垃圾回收篇十四--垃圾回收器
- Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?
- Sunyanfang, co-founder of WeiMiao: take compliance as the first essence and become the "regular army" of financial and business education
- How to sinicize the JMeter interface?
- Database design - relational data theory (ultra detailed)
- What should test / development programmers over 35 do? Many objective factors
- Quoted popular explanation
猜你喜欢

如何快速有效解决数据库连接失败问题

Use ngrok for intranet penetration

Use of collection framework

Idea remote debugging

Typescript details

JVM Part 1: memory and garbage collection part 7 -- runtime data area heap

整合SSM

JVM Part 1: memory and garbage collection part 14 -- garbage collector

Database design - relational data theory (ultra detailed)

SQL数据库→约束→设计→多表查询→事务
随机推荐
Database design - relational data theory (ultra detailed)
Static and final keyword learning demo exercise
2022 Zhengzhou light industry Freshmen's competition topic - I won't say if I'm killed
MQ FAQ
redis锁
数据库连接池&&Druid使用
The provision of operation and maintenance manager is significantly affected, and, for example, it is like an eep command
The difference between strlen and sizeof
JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter
2021 OWASP top 4: unsafe design
SQL数据库→约束→设计→多表查询→事务
LeetCode之268.Missing number
Solution and principle analysis of feign call missing request header
B1030 完美数列
笔记系列之docker安装Postgresql 14
Li Kou achieved the second largest result
Summary of knowledge points (I)
Select user stories | the false positive rate of hole state in jushuitan is almost 0. How to do this?
简化JDBC的MyBits框架
Integrate SSM