java learning -2

Class definition

Member variables:  Variable Type Variable Name

Members method: public void method name () {}

Note: The member variables are defined directly outside of class which method


Create an object with class

  1. Guide package. The need of the class in what position

import package name . Class name

If the class using the class in the same package which guide the package can not write

  1. Create , format;

Class Name object name = new class name ();

  1. When used

Use member variables: the object name . Member variable name

Use member method: object name . Member method name (parameters)


The difference between the member variables and local variables

1. Define the position is not the same

Member variables: External Methods

Local variables: internal Method

2. The scope of the role is not the same

Member variables: the whole class can use

Local variables: only internal methods


3. The default values are not the same

Member variables: with default values

Local variables: no default value, operation is not


By keyword private is not free to change the protected member variables

Note: By using private modified, although this class can be freely used, but beyond this class must getTer / setTer access variables and modify variables method.

Indirect access methods naming format getXxx / setXxx

getXxx : no arguments, return type and the same type of member variable

setXxx : no return value, the same parameter type member variable types

Exception: If the basic types of boolean type, getXxx should be changed isXxx format, setXxx unchanged.

this usage

When the member variables local variables and class methods of the same name, because the principle of proximity, will give priority to use local variables, when the need to access the member variable, you can add one in the front this

Format: . The this member variable






Construction method

Constructor method is designed to create an object, when we use the keyword new time to create the object, in fact, call the constructor.



Public class name (parameter type parameter name) {

The method body



  1. Constructor name and class name must be exactly the same.
  2. The method of construction does not require a return value
  3. Also can be overloaded constructor


Standard class and shortcuts

Standard class format:

  1. All member variables must be private modification.
  2. Each variable must be a member of a pair of Setter / Getter methods
  3. Writing a no-argument constructor
  4. Write a constructor parameter

A key generation Setter / Getter Method:

Lnsert + alt , select Setter and Getter , hold down the shift select all variables point ok

Write a constructor with no arguments:

Lnsert + alt , selection constructor , selection select none

Write a constructor argument:

Lnsert + alt , select the constructor , select all the variables, the ok

 Write a standard class



 Use a standard class


Guess you like