Java class entry notes

Source: Internet
Author: User
Tags class definition modifier

Concept

Class can be seen as a template for creating Java objects.

Classes are objective, abstract, and conceptual things.

The class keyword must be used to indicate that this is a class.

Since it is a class, java allows the class to be free from public keyword constraints. Of course, the class definition can only be public or unrestricted keywords (default ).

For example, a human:

Public class Person {
 
}

Or

Class Person {
 
}

In the above example, the public keyword is the access permission of this class, the class keyword indicates that this is a class, and the Person is the class name.

Main method

The declaration of the main () method is public static void main (String args []). This is a Java specification.

If a class contains the main () method and the "java class name" command is executed, the VM is started to execute the main method in the class.

When running this Java application, JVM will first call the main method. When calling this method, the object of this class is not instantiated, but directly called by class name. Therefore, it must be limited to public static.

Jvm has restrictions on the main method in java and cannot return values. Therefore, the return value type is void. The main method also has an input parameter of the String [] type, which is also a java specification. The main () method must have an input parameter, and the class must be a String [], as for the name of the string array, this can be set by yourself. According to the habit, the name of this string array is generally the same as that of the mian parameter in the sun java standard example, named args.

Therefore, the main () method must be defined as "public static void main (String array parameter name [])".

HelloWord instance:

Public class HelloWord {
Public static void main (String [] args ){
System. out. print ("HelloWord ");
    }
}


Access permission

In Java, access controllers can be used to protect access to classes, variables, methods, and constructor methods. Java supports four different access permissions.

Default access modifier-no keywords are used

All the variables in the interface are implicitly declared as public static final, and the access permission of the methods in the interface is public by default.

Note the rules inherited by the following methods:

1. The method declared as public in the parent class must also be public in the subclass.

2. The method declared as protected in the parent class is declared as either protected or public in the subclass. Cannot be declared as private.

3. The default modifier declaration method in the parent class, which can be declared as private in the subclass.

4. The method declared as private in the parent class cannot be inherited.

Instance:

Class Test1 {
Private static int age = 1;
Public static int age2 = 2;
Protected static int age3 = 3;
Static int age4 = 4;
}
 
Public class Test2 extends Test1 {
Public static void main (String [] args ){
// System. out. print (age); // no
System. out. print (age2); // yes
System. out. print (age3); // yes
System. out. print (age4); // yes
  }
}

Java class loading mechanism process

1. Pre-load and load as needed
The basic classes required for running Java are pre-loaded, and all the methods are loaded into the memory, because these units are frequently used during Java program running, including the rt of JRE. all. class file.
When we need to use the class defined by ourselves in the program, we need to use the loading method as needed.

2. Implicit loading and display loading

The new keyword is used in the program to define an instance variable. When JRE executes the new keyword, it loads the corresponding instance class into the memory and uses a lot of resources, the JRE system automatically loads data in the background, reducing users' workload and increasing system security and program readability.
Programmers write their own programs to load the required classes into the memory (display loading)

Class c = Class. forName ("TestClass ");
TestClass object = (TestClass) c. newInstance
We load the custom Class TestClass through the forName (String s) method of the Class, and initialize the instance through the newInstance () method.

Test test = new Test (); // The Test class is a custom Test class;

ClassLoader cl = test. getClass (). getClassLoader ();

// Obtain the class loader of test;

Class c = Class. forName ("TestClass", true, cl );

To load a class, you must have a loader. Here we get the loader cl for loading the Test class and use it as the class loader for loading the TestClass.


3. Custom class loading mechanism

URL url = new URL ("file:/d:/test/lib /");

URLClassLoader urlCL = new URLClassLoader (new URL [] {url });

Class c = urlCL. loadClass ("TestClassA ");

TestClassA object = (TestClassA) c. newInstance ();

Object. method ();


First, define the URL to specify where the class loader loads the class. The URL can point to any location on the Internet or to the file system (including JAR files) on our computer ). in the above example, we will look for classes from file:/d:/test/lib/. Then we will define URLClassLoader to load the required classes, and then we will use this instance.

4. Class loader class system
When hllo.classis executed, java.exe will automatically find a prominent jre location, and then find the jvm in it. dll (both jdk and jre have jvm. after the dll file is activated, the jvm is activated to load the dynamic library. After the jvm is activated, initialization is performed first, such as reading system parameters.

Then, once the initialization is complete, the first class loader will be generated ?? Bootstrap Loader is written by C ++. In the initial work of Bootstrap Loader, in addition to some basic initialization actions, the most important thing is to load Launcher. in java, and set its Parent to null to indicate that its Parent loader is BootstrapLoader. then Bootstrap Loader needs to load Launcher. appClassLoader in java, and set its Parent to the previously generated ExtClassLoader entity. Both loaders exist in the form of static classes. It should be noted that Launcher $ ExtClassLoader. class and Launcher $ AppClassLoader. class are both loaded by Bootstrap Loader, so there is no relationship between Parent and which class Loader is loaded.

These three loaders constitute our Java class loading system. They look for the classes required by the program from the following paths:

BootstrapLoader: sun. boot. class. path

ExtClassLoader: java. ext. dirs

AppClassLoader: java. class. path

The three System parameters can be obtained through the System. getProperty () function. You can view the specific path by programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.