java learning notes 2022.2.11

on java 8 excerpt (reflection)

  1. A basic goal of object-oriented programming is to write code that manipulates only references to the base class (in this case, Shape)

  2. The method draw() in the Shape interface can be dynamically bound, so the client programmer can call the specific draw() method through the generalized Shape reference. In all subclasses, draw() is overridden, and because it is a dynamically bound method, even calling it through a generalized Shape reference will produce the correct behavior. This is polymorphism.

  3. abstract class Shape {
      void draw() {
        System.out.println(this + ".draw()");
      }
      @Override public abstract String toString();
    }
    
    class Circle extends Shape {
      @Override public String toString() {
        return "Circle";
      }
    }
    
    class Square extends Shape {
      @Override public String toString() {
        return "Square";
      }
    }
    
    class Triangle extends Shape {
      @Override public String toString() {
        return "Triangle";
      }
    }
    public class Shapes {
      public static void main(String[] args) {
        Stream.of(
          new Circle(), new Square(), new Triangle())
          .forEach(Shape::draw);
      }
    }
    
  4. This is what reflection means: at run time, determine the type of object.

When the program first references the static member of this class, it will trigger the loading of this class. A constructor is a static method of a class, although the static keyword is not explicitly used. Therefore, using the new operator to create a new object of the class is also counted as a reference to the static member of the class. The first use of the constructor will lead to the loading of the class.

Once the Class object of this type is loaded into memory, it will be used to create all objects of this type.

// reflection/SweetShop.java
// Check how the class loader works

class Cookie {
static { System.out.println("Loading Cookie"); }
}

class Gum {
static { System.out.println("Loading Gum"); }
}

class Candy {
static { System.out.println("Loading Candy"); }
}

public class SweetShop {
public static void main(String[] args) {
 System.out.println("inside main");
 new Candy();
 System.out.println("After creating Candy");
 try {
   Class.forName("Gum");
 } catch(ClassNotFoundException e) {
   System.out.println("Couldn't find Gum");
 }
 System.out.println("After Class.forName(\"Gum\")");
 new Cookie();
 System.out.println("After creating Cookie");
}
}
/* Output:
inside main
Loading Candy
After creating Candy
Loading Gum
After Class.forName("Gum")
Loading Cookie
After creating Cookie
*/

I believe that at first, some people did not understand why this can be proved like me. In fact, through observation, we can find that the order of the above classes is Cookie, Gum and Candy. Below, I first created Candy, then Gum, and finally Cookie, and then because the static method will appear when the class is loaded for the first time, so according to the common sense, If they are compiled in advance rather than loaded dynamically, their output should be disordered or come down in writing order. I say disorderly because if they are loaded in advance, static code blocks will appear together, but which loaded block will be determined according to the compiler, which will lead to sequence problems; To sum up, since it comes down in the order of the main program, it must be loaded dynamically

Newinstance () is still normal in Java 8, but it has been deprecated in later versions. It is recommended to use constructor newInstance(). In the example, @ SuppressWarnings("deprecation") is used to suppress deprecation warnings of later versions.

on java 8 excerpt (reflection) (something you don't understand)

  1. In fact, the Class object is used to create all "general" objects of the Class.

I don't quite understand this sentence. What is a regular object?

We call forName() only for its side effect: if the class Gum has not been loaded, load it. During loading, the static code block of Gum is executed.

19.2 Class objects

  1. Class objects feel like static objects. They are loaded only once, followed by references to them

  2. About class Forname(), which is written in great detail https://www.geeksforgeeks.org/class-forname-method-in-java-with-examples/ And then the class name getclass(), the two calls are different

  3. I changed the on java program

    package test;
    class Cookie {
        static { System.out.println("Loading Cookie"); }
    }
    
    class Gum {
        static { System.out.println("Loading Gum"); }
    }
    
    class Candy {
        static { System.out.println("Loading Candy"); }
    }
    
    public class SweetShop {
        public static void main(String[] args) {
            System.out.println("inside main");
            new Candy();
            try {
                Class.forName("Candy");
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }
            System.out.println("After creating Candy");
            try {
               Class.forName("test.Gum");
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }
            System.out.println("After Class.forName(\"Gum\")");
            new Cookie();
            System.out.println("After creating Cookie");
        }
    }
    

other

  1. The toString() method exists because we need the string information of the class. Without this method, your instance object will pass its address
  2. Static members of a class are loaded only once, as are static code blocks
  3. In idea, note that the absolute address is not used when looking for a class, but the relative address For the code I gave above
  4. Looking at the reflection, the biggest feeling is that it can ignore the specific class programming, and the main framework is applicable

Keywords: Java

Added by t2birkey on Fri, 11 Feb 2022 20:47:17 +0200