Understanding of object-oriented understanding and this static

Source: Internet
Author: User

Object Understanding

The Java language is a direct object-oriented language, with previous
When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, when the scope of the variable is exceeded (for example, when function B is called in function A, the variable A is defined in function B, the scope of variable A is only function B, and the variable A is automatically destroyed when function B is finished. The memory allocated to it will be reclaimed, and Java will automatically release the memory space allocated for that variable, and the memory space can be used as an immediate alternative to this: its design starting point more directly describes the problem in the domain of the objective things.

Object uses the computer language to describe the things in the problem domain, and the object can correspond to the static and dynamic properties of things by means of "attributes" and "methods" respectively.

A class is an abstract concept used to describe an object of the same type, which defines the static and dynamic properties of an object of this class.

A class can see a template for a class of objects, that is, an abstract conversation, and an object can be seen as a concrete instance of the class, even if the class is instantiated. Such as


Or, a class is the blueprint for an object. He will tell the JVM how to create an object of a certain type. Objects created by a class will have their own instance variables. Eg: you can use a button class to create many sizes,

Different buttons for color, text, and so on. The general object is the instantiation of the class, such as.

The advantages of facing the object:

A more advanced programming language, more convenient for people to communicate with the computer, so that people's programming and the real world closer, all the objects are endowed with properties and methods, more humane.

B compared to the process-oriented language, the object language is much easier to modify, modify the part only in the local module, unlike C + + needs global analysis and modification

Design Ideas for objects:

Remember everything is the object!! Everything is the object of everything!! Everything is the object of everything!!

When designing a class, remember that the object is shaped by the model of the class. Objects known to the object are called I: instance variables, which are both properties of the object;

The actions an object can perform are called: methods. Like what:

public class dogtest{

public static void Main (String [] args) {

Dog dog = new Dog ();//Create a Dog object

Dog.name = "F";//Access the object's variable value, that is, the property value

Dog.play ();//Call method to let the dog play

}

}

Class dog{

Sring name;//Declares the property that this object contains the name of the dog

int size;//Declare the size of the dog

void Play () {//methods that can be called

System.out.println ("Run,run");

}

}


To remember the principle of the Main method Main,main generally only need to do two things: test the real class; Start your Java program

Real Java programs only let objects interact with objects, and main just creates objects and starts programs!

The understanding of this

Simple point: it can refer to the class's properties and methods within the class. Can appear in the instance method and in the construction method



However, this cannot occur in a class method, because the class method can be called directly through the class name, and there may not be any objects being new.

If you want to use member variables within an instance method or a class method, for instance methods, the "This" in front of the member variable cannot be omitted; For class methods, class variables must be manipulated using the class name.

The understanding of Static:

Static represents a global or static meaning that is used to modify member variables and member methods.

This allows the class file to be loaded, first allocating memory to the content modified by static, so it can be accessed before any objects are created.

The static modified member variables and member methods are customarily referred to as static variables and static methods, which can be accessed directly through the class name (no instantiation required), and the Access syntax is:
Class name. static method Name (parameter list ...)
Class name. Static variable Name

In a class, member variables declared with static are static member variables, also known as class variables, and the difference between member variables is that the JVM allocates memory only once for static.

The method of the static declaration is called a static method or a class method.

When a method is called, a reference to the object is not passed to him, so a non-static member cannot be accessed in the static method.



Memory resolution and Address:

A complete Java program will run with the following major memory areas involved:

1, stack memory space: holds the value of the local variable, including: 1 The value used to hold the base data type 2 holds the instance of the class, that is, the application of the heap area object.

can also be used to save the frame when loading mode.

When a variable is defined in a block of code, Java allocates the memory space for the variable in the stack, when the scope of the variable is exceeded

(for example, when function B is called in function A, the variable A is defined in function B, the scope of variable A is only function B, and after function B has finished running,

Variable A is automatically destroyed. The memory allocated to it is reclaimed, and Java automatically frees up the allocated memory space for that variable, which can be used as an immediate alternative.

2, heap Memory: used to store dynamically generated data, such as new objects. Note the objects that are created contain only the member variables that belong to them, and do not include member methods.

Because objects of the same class have their own member variables stored in their own heap, they share the methods of the class, and not every object is created to copy the member method one at a time.

3, Global Data area: Saves all properties of the static type, prioritizes memory once the JVM executes

4, Global Code area: maintains all method definitions.

5, Constant pool: The JVM maintains a constant pool for each loaded type, and a constant pool is an ordered set of constants used by this type. Includes direct constants (base type, String)

And a symbolic reference to other types, methods, and fields (1). The data in the pool is accessed through the same index as the array. Because the constant pool contains a type of all of the other types, methods, fields

Symbolic reference, so Chang plays a central role in the dynamic link of Java. A constant pool exists in the heap.






eg



It is preferred to store the static SID in the data area, Cat.sid = 100, assign the value to Sid=100, and create 2 Cat objects Mimi and Pipi respectively, and the stack memory is also assigned a variable Mimi, the same stack memory Mimi can be found

A new object in the heap memory (like a pointer), a string in which the program also passes the value "Mimi", the string allocates memory in the data area, and when the constructor is called, temporarily allocates memory for the parameter name in the stack memory and points to the string Mimi,this.name =name, The name in the object is also pointed to "Mimi", after which the constructor completes and name disappears.



Similarly pipi memory is the same! Where the SID has its count effect.


Summary: To learn to analyze memory, understand the object! The design idea of the learning object



Understanding of object-oriented understanding and this static

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.