What is static in Java? And the role of static

Static means “global” or “static” and is used to modify member variables and member methods. It can also form a static code block, but there is no concept of global variables in the Java language.

Let’s first look at the following program

private String name;
private int age ;
String city="A city";
\t
public Person() {
}
\t
public Person(String name, int age) {
this.name= name;
this.age = age;
}
public String fun(){
return "Name:" + this.name + "Age:" + this.age + "City:" + city;
}
\t
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
\t
}
public class StaticDemo {
public static void main(String[] args) {
Person p1 = new Person("MOHAN",19);
Person p2 = new Person("damiao",17);
Person p3 = new Person("dez",23);
System.out.println(p1.fun());
System.out.println(p2.fun());
System.out.println(p3.fun());
}
}

Output result:

Name: MOHAN Age: 19 City: City A

Name: damiao Age: 17 City: City A

Name: dez Age: 23 City: City A

Then here comes the problem!

·The information represented by the city attributes is the same, and the content is repeated for each object.

·If we assume that city A is changed to city B, then 500 objects are generated at this time, then we will modify it 500 times. The best way to solve the problem now is to set city as a public attribute

Let’s look at the code

class Person2{
private String name;
private int age ;
String city="A city";
\t
public Person2() {
}
\t
public Person2(String name, int age) {
this.name= name;
this.age = age;
}
public String fun(){
return "Name:" + this.name + "Age:" + this.age + "City:" + city;
}
\t
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
\t
}
public class StaticDemo2 {
public static void main(String[] args) {
Person2 p1 = new Person2("MOHAN",19);
Person2 p2 = new Person2("damiao",17);
Person2 p3 = new Person2("dez",23);
p1.city="B city";
System.out.println(p1.fun());
System.out.println(p2.fun());
System.out.println(p3.fun());
}
}

Then the output result at this time

Name: MOHAN Age: 19 City: B City

Name: damiao Age: 17 City: City A

Name: dez Age: 23 City: City A

We found that only p1 was changed to city B.

For example, if we want to set a property to a public property, we need to use the static keyword to declare it.

class Person2{
private String name;
private int age ;
static String city="A city";
\t
public Person2() {
}
\t
public Person2(String name, int age) {
this.name= name;
this.age = age;
}
public String fun(){
return "Name:" + this.name + "Age:" + this.age + "City:" + city;
}
\t
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
\t
}
public class StaticDemo2 {
public static void main(String[] args) {
Person2 p1 = new Person2("MOHAN",19);
Person2 p2 = new Person2("damiao",17);
Person2 p3 = new Person2("dez",23);
p1.city="B city";
System.out.println(p1.fun());
System.out.println(p2.fun());
System.out.println(p3.fun());
}
}

Output result:

Name: MOHAN Age: 19 City: B City

Name: damiao Age: 17 City: B city

Name: dez Age: 23 City: B city

We can see that all the cities have been changed to City B. Let’s take a look at the addresses in the memory.

We can see that these three objects all point to the city attribute. When we reassign the value of city, it will change to city B.

Summary: Static member variables and member methods modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of the static variable is not generated, but all instances of the class share the same static variable.

Next let’s look at the static modification method

Static methods can be called directly through the class name, and any instance can also be called.
Therefore, the this and super keywords cannot be used in static methods.

class Person3{
private String name;
private int age ;
private static String city="A city";
\t
public static void setCity(String c){
city = c;
}
\t
public Person3() {
}
\t
public Person3(String name, int age) {
this.name= name;
this.age = age;
}
public String fun(){
return "Name:" + this.name + "Age:" + this.age + "City:" + city;
}
\t
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class StaticDemo3 {
public static void main(String[] args) {
Person3 p1 = new Person3("MOHAN",19);
Person3 p2 = new Person3("damiao",17);
Person3 p3 = new Person3("dez",23);
Person3.setCity("B City");
System.out.println(p1.fun());
System.out.println(p2.fun());
System.out.println(p3.fun());
}
}

At this time our output is:

Name: MOHAN Age: 19 City: B City

Name: damiao Age: 17 City: B city

Name: dez Age: 23 City: B city

Let’s say that if a person says that this city has been changed to City B, he has to tell everyone in City A. If everyone in the entire city says that this is City B, it will be a lot easier for this person.

Look at this program again

class Person3{
private String name;
private int age ;
private static String city="A city";
\t
public static void setCity(String c){
city = c;
this.name= name;
}
\t
public Person3() {
}
\t
public Person3(String name, int age) {
this.name= name;
this.age = age;
}
public String fun(){
return "Name:" + this.name + "Age:" + this.age + "City:" + city;
}
\t
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class StaticDemo3 {
public static void main(String[] args) {
Person3 p1 = new Person3("MOHAN",19);
Person3 p2 = new Person3("damiao",17);
Person3 p3 = new Person3("dez",23);
Person3.setCity("B City");
System.out.println(p1.fun());
System.out.println(p2.fun());
System.out.println(p3.fun());
}
}

This program will report an error:

Exception in thread “main” java.lang.Error: Unresolved compilation problems:

Cannot use this in a static context

Cannot make a static reference to the non-static field name

meaning is:

You cannot use it in a static context

Cannot make a static reference to a non-static field name

Remember: Static methods can only call static methods and variables, they cannot call non-static methods and variables.

Non-static methods can call our static methods and variables only after instantiation.

Static properties and methods can be called without instantiating the object.

Common attributes and methods in a class. Can only be called after instantiation.

Let’s talk about the main method. Our main method also has the static keyword. What does it mean?

public static void main(String args []){

  • public: Maximum permissions, accessible to everyone.
  • static: The execution method is the name of the execution class, which means it can be called directly by the class name.
  • void: The main method is the starting point of everything, so whether there is a return value is equivalent to whether a person can go back after he is born?
  • main: This is the name of the method built into our system.
  • String args []: represents a string array. Used to receive parameters

}

public class StaticDemo4 {
public static void main(String args[]) {
for(int i=0;i<args.length;i + + ){
System.out.println(args[i] + ",");
}
}
}

The output format of this program

Java StaticDemo4 Parameter 1 Parameter 2 Parameter 3…There must be spaces in the middle.

For example, directly output hello world

The output result is: hello,

world,

Then if you want to output hello world, you need “hello world”

Then the output result at this time is: hello world.

Program memory division at this time:

  • Stack memory: The object name is actually the reference address of the object to the heap.
  • Heap memory: properties.
  • Global code area: saves all operation methods.
  • Global data area: saves all static attributes.
Static members cannot be accessed by instances created by the class in which they are located.
If the members without static modification are object members, they are owned by each object.
Members modified with static are class members, which can be directly called by a class and are common to all objects. 

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139134 people are learning the system