Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the composition structure of java class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to understand the composition and structure of the java class, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Member variable

A member variable is a variable defined in a class.

For example:

Public class POP {int a = 1;}

There are two kinds of member variables, one is the class variable modified by static, which is classified as all, and the other is the instance variable that is not modified by static, which belongs to the object.

As follows:

Public class POP {int a = 1; static int b = 1;}

As literally, categorize all the variables b, which can operate directly in the form of POP.b outside the class.

The procedure is as follows:

Public static void main (String [] args) {POP.b++; System.out.println (POP.b);}

However, in practice, we can also call him in the form of object .b.

As follows:

Public static void main (String [] args) {POP.b++; System.out.println (POP.b); System.out.println (new POP () .b);}

This is a very bad defect of java, because this variable b, actually belongs to the class, although it can be called with an object, but it is not recommended.

Understand again that there is only one copy of this variable b, as follows:

Public static void main (String [] args) {POP.b++; System.out.println (POP.b); System.out.println (new POP () .b + +); System.out.println (new POP () .b + +);}

You will find that although I use different objects to operate on b, their operations work together on the same b, which is why I do not recommend using objects to call class variables.

Member variables can be modified by four permission modifiers to suit a variety of applicable situations.

Member variables can be modified by final as constants.

Member variables can also be modified by the static I mentioned above.

Attach the general code:

Public class POP {public static void main (String [] args) {POP.b++; System.out.println (POP.b); System.out.println (new POP () .b + +); System.out.println (new POP () .b + +);} int a = 1; static int b = 1;} method

Methods are divided into instance methods and class methods according to the modification by static.

Public void get () {} public static void did () {}

It is important to note that instance methods can call class variables, while class methods cannot call instance variables. why?

We can think about it that class methods are owned by classes and instance methods are owned by objects.

When the class exists, the object is not necessarily created, and if the instance variable is called through the class method, the instance variable does not exist, so this is not desirable.

Methods can be modified by four permission modifiers.

Methods can be modified by static.

Methods can be modified by final, indicating that they cannot be overridden by subclasses.

A method can be modified by abstract to indicate that it is an abstract method, and as long as there is an abstract method, the class is an abstract class.

We'll talk about abstract classes later.

Constructor

A constructor for creating objects.

Can be modified by four permission modifiers.

Protected POP () {} POP (int a) {}

We can call other constructors in the constructor in the form of this.POP (formal parameter list). Note that in this way, it must be written in the top part of the method body, and if this is not written, java will default to super to call the empty parameter constructor of the parent class, which is why we suggest that we design the program.

Always leave an empty parameter constructor.

So why should we call another constructor through this in one constructor?

Because this can reduce the duplication of code, it is convenient for us to iterate and upgrade.

Also note that the constructor does not return a value, of course, you can also say that its return value is the object he created, but we cannot indicate it.

Code block

The code block is divided into instance code block and class code block.

{System.out.println (instance code block);} static {System.out.println (class code block);}

The class code block belongs to the class and is executed when the class is loaded, while the instance code block is executed when the object is created.

On how to understand the structure of the java class to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report