static keyword, abstract class, interface, internal class

Posted by fernandodonster on Wed, 12 Jan 2022 11:46:32 +0100

static keyword explanation

  • package com.mao.oop.demo07;
    public class Student {
        private static int age;//Static variable multithreading!
        private double score;//Non static variable
        public void run(){
            go();//Non static methods can call things in static methods
        public static void go(){
        public static void main(String[] args) {
            go();//Static methods can call static methods, but non static methods cannot be called
    package com.mao.oop.demo07;
    public class Person {
        //2: Assign initial value
            //Code block (anonymous code block)
            System.out.println("Anonymous code block");
        //1: Execute only once
        static {
            //Static code block
            System.out.println("Static code block");
        public Person(){
            System.out.println("Construction method");
        public static void main(String[] args) {
            Person person1 = new Person();
            Person person2 = new Person();
    package com.mao.oop.demo07;
    //Static import package~
    import static java.lang.Math.random;
    import static java.lang.Math.PI;
    public class Test {
        public static void main(String[] args) {

abstract class

  • The abstract modifier can be used to modify a method or a class. If a method is modified, the method is an abstract method; If you modify a class, it is an abstract class

  • Abstract classes can have no abstract methods, but classes with abstract methods must be declared as abstract classes

  • An abstract class cannot use the new keyword to create an object. It is used to allow subclasses to inherit

  • Abstract methods have only method declarations and no method implementations. They are used to implement subclasses

  • If a subclass inherits an abstract class, it must implement an abstract method that the abstract class does not implement, otherwise the subclass must also be declared as an abstract class

package com.mao.oop.demo08;

//Abstract abstract class: class extensions: single inheritance interface can inherit more than one
public abstract class Action {

    //Constraint ~ someone helped us achieve it
    //Abstract, abstract method, only method name, no method implementation
    public abstract void doSomething();

    //1. You can't use the abstract class new. You can only rely on subclasses to implement it: constraints!
    //2. Ordinary methods can be written in abstract classes
    //3. Abstract methods must be in abstract classes
    //Abstract: Constraints

    //An abstract class cannot be a new object. Does it have a constructor?
    //What is the meaning of abstract classes? Improve development efficiency

package com.mao.oop.demo08;

//All methods of an abstract class that inherit its subclasses must implement its methods ~ unless~
public class A extends Action {
    public void doSomething() {



  • Common class: only concrete implementation

  • Abstract classes: concrete implementations and specifications (abstract methods) are available

  • Interface: specification only

  • An interface is a specification. It defines a set of rules, which embodies the idea of "if you are... You must be able to..." in the real world.

  • The essence of interface is contract, just like our law. After making it, everyone will abide by it

  • The essence of OO is the abstraction of objects. The interface can best reflect this. Why we discuss design patterns only for languages with abstract ability (such as C + +, java, c# etc.) is because what design patterns study is actually how to abstract reasonably

  • The keyword for declaring a class is class, and the keyword for declaring an interface is interface

package com.mao.oop.demo09;

//Abstract thinking ~ Java
//The keyword defined by interface and the interface need to have an implementation class
public interface UserService {

    //Constant ~ public static final
    int AGE = 99;

    //All definitions in the interface are actually Abstract public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);

package com.mao.oop.demo09;

public class TimeService {
    void timer() ;

package com.mao.oop.demo09;

//Abstract class: Extensions
//Class can implement the implements interface
//If you implement the class of the interface, you need to rewrite the methods in the interface

//Multi inheritance ~ realize multi inheritance by using interface
public class UserServiceImpl implements UserService,TimeService {
    public void add(String name) {


    public void delete(String name) {


    public void update(String name) {


    public void query(String name) {


    void timer() {

    1.A constraint
    2.Define some methods for different people to implement~
    3.public abstract
    4.public static final
    5.The interface cannot be instantiated ~, there is no constructor in the interface~
    6.implement Multiple interfaces can be implemented
    7.You must override the methods in the interface~

Inner class

  • An internal class is to define another class within a class. For example, if a class B is defined in class A, class B is called an internal class relative to class A, and class A is an external class relative to class B

    1. Member inner class

      • package com.mao.oop.demo10;
        //A java class can have multiple class classes, but only one public class
        public class Outer {
            private int id = 10;
            public void out(){
                System.out.println("This is the method of an external class");
            public class Inner{
                public void in(){
                    System.out.println("This is the method of the inner class");
                //Get the private properties of the external class
                public void getID(){
        package com.mao.oop;
        import com.mao.oop.demo10.Outer;
        //There is only one main method per project
        public class Application {
            public static void main(String[] args) {
                Outer outer = new Outer();
                //Instantiate the inner class through this outer class
                Outer.Inner inner = Inner();
    2. Static inner class

    3. Local inner class

    4. Anonymous Inner Class

      • There is no name to initialize the class, so there is no need to save the instance to the variable

      • package com.mao.oop.demo10;
        public class Text {
            public static void main(String[] args) {
                //Anonymous inner class: there is no name to initialize the class, and there is no need to save the instance to the variable~
                new Apple().eat();
                UserService userService = new UserService() {
                    public void hello() {
            class Apple{
                public void eat(){
            interface UserService{
                void hello();

Topics: Java JavaEE