* Declaration of the class
[Modifier] Class < class name > [extends parent class name] [Implements interface list]{}
Modifiers: optional, for specifying access rights, the available values are public,abstract and finnal.
Class name: In general, the first letter is required to be capitalized.
Extends parent class name: optional, which specifies which parent class the class to be defined inherits from.
Implements interface list: used to specify which interfaces the class implements.
A class that is declared public indicates that the class can be accessed and referenced by all other classes, meaning that other parts of the program can create objects of that class, access the member variables visible inside the class, and the visible methods that call it.
Instance
Import java.awt.*;
Import javax.swing.*;
public class Grilayoutdemo extends jframe{
Private JFrame frame;
Private JButton B1, B2, B3, B4, B5, B6;
Public Grilayoutdemo () {
Super ();
frame = new JFrame ("grid example");
Container ContentPane = Frame.getcontentpane ();
Contentpane.setlayout (New GridLayout (3, 2));
B1 = new JButton ("Grid_1");
b2 = new JButton ("Grid_2");
B3 = new JButton ("Grid_3");
B4 = new JButton ("Grid_4");
B5 = new JButton ("Grid_5");
B6 = new JButton ("Grid_6");
Contentpane.add (B1);
Contentpane.add (B2);
Contentpane.add (B3);
Contentpane.add (B4);
Contentpane.add (B5);
Contentpane.add (B6);
Frame.pack ();
Frame.setvisible (TRUE);
}
public static void Main (string args[]) {
Grilayoutdemo that = new Grilayoutdemo ();
}
}
* Member method
[Modifier] < method return value type > < method name > ([parameter list]) {[Method body]}
The class includes member variables and method two parts. A member variable represents the property of a class, the form of a data field, and the method is the definition of the operation of the data.
In Java, a method can only be a member of a class, also known as a member method. method to manipulate the data defined by the class and to provide access to the data. In most cases, other parts of the program interact through the methods of the class and the instances of other classes.
In a class, all but the variables are member methods.
One of the methods is to initialize a member variable, called a construction method. Outside the construction method is the general method of it.
Therefore, the member method is divided into the construction method and the general method.
* Member Variable
[Modifier] [Static] [Final] < variable type > < variable name >
static{
}
Static initialization block: A block of code that is defined with a keyword static. Once the class is loaded into the system, the static initialization block can only initialize static data members of the class.
There are three places in 1 Java classes where you can place code that performs an action: method, constructor, initialization block.
Completely unrelated concepts.
Modifier: An optional parameter that specifies that the variable is accessed, and the optional value is public,protected and private.
Static: optional, used to specify that the member variable is a static variable and can be accessed directly from the class name.
Final: optional, constant specifying that the member variable will not change for a value
* Local Variables
[Final] < variable type > < variable name >;
Final: Optional, used to specify that the local variable is constant.
* Valid range of variables
Member variables: Declared in a class, valid throughout the class.
Local variables: Variables declared within a method or in a compound code block within a method. Valid only in the current composite block.