java static variables, methods, constants

1. The meaning of existence
In the process of our learning, such as the acceleration of gravity g, the circle rate π, if we need to use π in the ellipse class, we also need to use π in the circle class, if we repeatedly create the quantity π First of all, it is very troublesome and wastes memory. Some people may say that the ellipse can be inherited from the circle class, but in some cases this method is not applicable, so we introduced static
2.

import static java.lang.System.out;
public class staticxx {
    
    
	static int x=999;
	public class yf{
    
    
		final static int y=222;
		
	}
     static void love()
     {
    
    
    	System.out.println("喜欢你"); 
     }
	
	public static void main (String[] args) {
    
    
	
        out.println(staticxx.x);
        out.println(staticxx.yf.y);
        staticxx.love();

	}

}


operation result
Insert picture description here

It can be seen that we did not instantiate the class but directly call the variables through the class name. The careful students may find that I added a final before the definition of y in the yf class. Why, because the yf class is not a static class , We know that when initializing a class, the static modified code will be executed first, because the yf class is not a static class, the first thing we execute is "final static int y=222;" but because the class it is in is not initialized, it will Report an error.

Of course, we can also call static variables through the object

import static java.lang.System.out;
public class staticxx {
    
    
	static int x=999;
	public class yf{
    
    
		final static int y=222;
		
	}
     static void love()
     {
    
    
    	System.out.println("喜欢你"); 
     }
	
	public static void main (String[] args) {
    
    
	
       staticxx love=new staticxx();
       yf you=love.new yf();
       System.out.println(love.x);
       System.out.println(you.y);
	}

}

But we generally do not recommend this use, because it is easy to confuse static members and non-static members.

Through the above code, we can see that an internal class needs a reference to an external class to be created, but a static internal class can be created directly

import static java.lang.System.out;
public class staticxx {
    
    
	static int x=999;
	public static class yf{
    
    
		final static int y=222;
		
	}
     static void love()
     {
    
    
    	System.out.println("喜欢你"); 
     }
	
	public static void main (String[] args) {
    
    
	
      yf yjl=new yf();
      System.out.println(yf.y);
	}

}

The above are the basic methods;
here are a few forgotten and error-prone knowledge points:
1. Non-static members can be called in static classes, but not in static methods, and the this keyword cannot be used;
2. Can not be defined in non-static classes Static members (can be modified by final);
3. Static members can not be declared in the method body (static methods are not acceptable) ;

Guess you like

Origin blog.csdn.net/jahup/article/details/105917866