Java learning: inner class, static keyword, final, exception handling

Inner class

How to define an internal class? The internal class can directly access any properties and methods (including private) of the external class surrounding it
In general, the purpose of defining an inner class is to use it in the outer class that wraps it.

Interview questions:
//If the inner class object is initialized outside
Inner01 t1 = new TestInClass().new Inner01();
t1.say();
//If it is a static inner class, you can create it in the following way
Inner02 t2 = new TestInClass.Inner02();

public class TestInClass {
	
	private int id = 10;
	private static String name;
	
	public void say() {
		Inner01 t = new Inner01();
		t.say();
		
	}
	
	
	// Inner01 is the inner class of TestInClass
	public class Inner01 {
		
		public void say() {
			System.out.println(id);
			System.out.println(name);
			
		}
	}
	
	// Static class (static inner class)
	public static class Inner02 extends Outer {
		public void say() {
			TestInClass tic = new TestInClass();
			System.out.println(tic.id);
			System.out.println(name);
		}
	}

}

static keyword

Static: static
You can modify java classes, properties, and methods:

  • static can only modify inner classes if it modifies classes
  • You can also modify an attribute. If you use static to modify an attribute, it is a static attribute and belongs to a class
  • You can also modify methods, static methods, which belong to classes.
  • Static can also be used directly and independently. The syntax blocks used separately by static are static blocks
  • Static block: static block will be loaded by the class first and only once!!!
  • static can also modify import
    import static package*
    static decorated things are loaded into memory in advance

final keyword

  • final modifies a variable, which is called a constant and cannot be modified at runtime.
  • Final can modify a class. Once final modifies a class, the class is the final class and cannot be inherited.
  • Final can also modify the method, which is the final method and does not allow overriding

exception handling

Exception refers to the abnormal (improper user operation, service termination, program BUG...) program that cannot operate normally during the operation of the program
The purpose of exception handling: in order to improve the stability and robustness of the program and make the program operate normally.

Exception handling method:

  • Catch exceptions: try to catch the corresponding exceptions and deal with specific problems

try {
//Code block
}catch (XxxException e) {/ / when an exception is caught, the exception object will be created automatically. e is the reference object
//Handling exceptions
} catch (XxxException e) {
//Handling exceptions
}Finally {/ / finally keyword: important and must be executed, such as resource release, garbage collection, etc. whether there are exceptions or not, the finally part must be executed
}
e.getMessage() / / output exception information
e.printStackTrace() / / print out the abnormal error condition (calling stack condition) on the console
----try syntax fast:
1. If no exception is caught, the program will execute normally, and the catch syntax will not enter quickly!!!
2. If a line of code is triggered once in the try, the program immediately enters the corresponding exception handling block (catch) for exception handling

	private static int test02() {
		int num = 10;
		try {
			System.out.println(num);
			return num;
		} catch (Exception e) {//When an exception is caught, the exception object will be created automatically. e is the reference object
			num += 10;
			System.out.println("exception occurred");
		} finally {
			num++;
			System.out.println("Code that must be executed");
		}
		return num;
	}
  • Throw exception: throw up

An exception is thrown and solved by the upper layer
Throw exception name after method name{
}
Once the throw scheme is used
The exception will be resolved by the caller

public static void main(String[] args) {
	//Resolve the thrown exception
			add(-1, 0);
	
		System.out.println("1234546");
	}
	
	public static int add(int a, int b) throws Exception{ //Throw the exception to the upper layer code for resolution, and throw keyword throws the exception,
	                                                      //throw exception object
		if (a < 0 || b < 0) {
			throw new MyException("Sorry, both numbers must be positive");
		}
		int c = a + b;
		return c;
	}
}
  • Inheritance relationship of java exception class:
    • Throwable
      • Error
      • Exception
        • RuntimeException
        • Non runtime exception
        • IOException
  • Use of custom exception classes:

During project development, a project usually needs to customize an exception to pass information
In java, custom exceptions are very simple. As long as you inherit the Exception class (called a subclass of Exception), this class is an Exception class
If two custom numbers are not positive, it is an exception

public static void main(String[] args) {
		try {
			add(-1, 0);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
		System.out.println("1234546");
	}
	
	public static int add(int a, int b)  {
		if (a < 0 || b < 0) {
			throw new MyException("Sorry, both numbers must be positive");
		}
		int c = a + b;
		return c;
	}
  • Classification of exceptions:

1. Runtime exception: it inherits the exception of RuntimeException. You can only know whether there will be an exception at runtime
2. Non runtime exception (compiled exception): an error will be reported during compilation and must be handled!!!, For example, syntax errors at compile time.

  • Related interview questions: find the operation results in the figure below

    You should execute the code in finally first, and then return the end function body according to the return in try. Because the value of return has already been set, the result will not change
    The operation results are as follows:

Keywords: Java

Added by Matty999555 on Tue, 18 Jan 2022 14:21:17 +0200