Java learning notes 2 object-oriented (on), java learning notes

Source: Internet
Author: User

Java learning notes 2 object-oriented (on), java learning notes

Class and Object

A class is the abstraction of a batch of objects. It can be understood as a concept. An object is a specific entity. Class and object are the core of object-oriented.

A class defines the characteristics of multiple instances. A class does not exist, but an instance does.

Class)Syntax:

[Modifier] class name

{Zero to multiple constructors define ....

Zero to multiple member variables ....

Zero to multiple methods ....}

Key points: 1. Three common members: Constructor (new Keyword call) member variable method

2. The class name is composed of one or more meaningful words. The first letter of each word is capitalized, And the other lowercase letters are used.

3. the modifier can be public final abstract or omitted

 

Define a member variable (field)Syntax:

[Modifier] type member variable name [= default value];

Key points: 1. the first letter of the member variable name is lowercase, followed by the first letter of each word, and the rest are lowercase.

2. the type can be basic or reference type.

 

Definition MethodSyntax

[Modifier] method return value type method name (parameter list)

{Method body}

Key point: The method name and the rules of the member variables are the same. Generally, the English verb is selected.

StaticIt is a special keyword that can be used to modify the member variables of a method. Static modified members belong to a single instance of the class rather than the class, also known as class variables and instance variables (static variables and non-static variables ).Static members cannot access non-static members.

ConstructorIs a special method.

[Modifier] constructor name (parameter list)

{Method body}

Key points:The constructor name must be the same as the class name.

Class: defines variables to create objects, call class methods, or define class variables.

 

Generation and use of objects

The fundamental way to create an object is the constructor. You can call the constructor of a class through the new keyword to create an instance of this class.

For example, Persn p = new Person (); // defines the p variable and assigns a value to the p variable.

Class or instance access method and member variable Syntax: class. Class variable | Method Instance. instance variable | Method

Static modified member variables can be called through classes and instances. Without static modification methods and member variables, they can only be called through instances.

 

Person p = new Person (); this Code creates the Person object, which is assigned to the p variable.

 

The person object is assigned to a variable

Person Type VariableActually a reference is stored in the stack memory. Point to the actual person object

Real person objects are stored in heap memory.

Java programs do not allow direct access to objects in the heap memory. They can only operate on this object through reference of this object. when accessing p to reference member variables and methods of variables, it is actually a member variable and method that accesses the object referenced by p.

 

This reference of the object

The biggest function of this keyword is to allow a method in the class to access another method or instance variable in the class.

This keyword always points to the object that calls this method.

Most of the time, a method accesses other methods defined in this class. The effect of adding this prefix to a member variable is exactly the same.

 

MethodMethods cannot be defined independently. Methods must belong to Classes or objects.

When a method in the same class calls another method, if the called method is a common method, this is used as the called by default. If the called method is static, the class is used as the caller by default.

In java, there is only one type of parameter passing: Value passing passes a copy of the actual parameter into the method, and the parameter itself is not affected.

Parameter copies are classified into basic type parameters and reference type (Address) parameters.

1 public class PrimitiveTransferTest 2 {3 public static void swap (int a, int B) 4 {7 int tmp = a; 8 a = B; 10 B = tmp; 12 System. out. println ("in the swap method, the value of a is" 13 + a + "; the value of B is" + B);} 14 public static void main (String [] args) 16 {17 int a = 6; 18 int B = 9; 19 swap (a, B); 20 System. out. println ("after the exchange ends, the value of variable a is" 21 + a + "; the value of variable B is" + B );}

1 class DataWrap 2 {3 int a; 4 int B; 5} 6 public class ReferenceTransferTest 7 {8 public static void swap (DataWrap dw) 9 {10 // The following three lines of code exchange the values of member variables a and B of dw. 11 int tmp = dw. a; 13 dw. a = dw. b; 15 dw. B = tmp; 17 System. out. println ("in the swap method, the value of a member variable is" 18 + dw. a + "; the value of the B member variable is" + dw. b); 19 // assign dw to null directly so that it no longer points to any valid address. 20 dw = null; 21} 22 public static void main (String [] args) 23 {24 DataWrap dw = new DataWrap (); 25 dw. a = 6; 26 dw. B = 9; 27 swap (dw); 28 System. out. println ("after the exchange ends, the value of the member variable is" 29 + dw. a + "; the value of the B member variable is" + dw. b); 30}

 

Member variables and local variablesA member variable is a variable defined in a class. A local variable is a variable defined in a real method.

As long as the class exists, the program can access the class variable class of the class. Class variable

As long as the instance exists, the program can access the instance variable instance. instance variable of the instance.

Class variables can also allow instances of this class to access the instance. class variables

Modifying the value of a class variable through an instance causes other instances of this class to obtain the modified value when accessing this class variable.

 

Java allows local variables and member variables to have the same name. If the local variables in the method have the same name as the member variables, the local variables overwrite the member variables. If you need to call the override member variables in the method, you can call this (instance variable) or Class Name (class variable.

Running Mechanism of member variables in memory

 

 

Related Article

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.