Java: Class and Object concepts

Source: Internet
Author: User

 
    • What is a class?
    • How to define a class?
    • What is an object,
    • object references and objects in memory are as What is assigned?
    • What is a class?
      1. A class is an abstract collection of things that have the same attributes and functions, and in object-oriented programming, the concept of class is obtained by combining the static properties of a class of things with the actions that can be performed dynamically.
2. A class is an abstract concept used to simulate a class of things that, once defined, will always exist. For example: People, people have men and women, they have the same height, weight and other properties, they all have walking, eating, work and other these same, can be executed dynamically, so you can define "human" such a class to contain their same properties and functions.  
    • How do I define a class?
in Java , you define a class using the class keyword, as follows: Define a person class
1 /**2     @authorzst3     @version1.04 */5  Public classperson{6     PrivateString name;7     Private intAge ;8     9      Public voidWork () {Ten          One     } A}
Description: 1. The first letter of the class name is capitalized, and if the class name to be defined consists of multiple words, the first letter of each word is capitalized 2. If the method is publicly available, it needs to be modified with the "public" keyword in the person class above: 1. Name,age is called a class data member, or a field 2. work () is called a member function of a class, or a method
 
    • What is an object, What is the relationship between a class and an object, How do I create an object?
1. An object is an individual of a class, is a concrete, real thing, it has its own life cycle, the class can create objects, objects will die.
2. Once the class is established, it is possible to create any object with this class, then manipulate the objects, the class is abstract and the object is concrete.
3. The object is also called an instance and is a real entity.    Relationship: 1. If the object is compared to the real existence of a car, then the class is the design drawings of the car, of course, through the design drawings (class) can also make a second car, the third car (object).          2. As above, through the person class can create an object called "Xiao Ming", you can also create another called "Yellow" object.
  The process of creating an object is also called instantiation of the class, and the new keyword is used to create the object.  1 Person xiaoMing = new Perosn(); As above is the process of creating an object: Two steps are performed:1.Person xiaoming: declares a call Xiaoming'sthe object reference for the person class. 2.Newperosn(): creates a person object, at which point Xiaoming points to the created object and oftenXiaoming is called the object name.  
    • How are object references and objects allocated in memory?
1 /**2     @authorzst3     @version1.04 */5 classA6 {7     inti = 10;8     intj = 10;9      Public Static intK = 20;Ten      One      Public voidf () A     { -System.out.println ("I am a method of Class A object"); -     } the } -  Public classobjectmemoryallocation - { -      Public Static voidMain (string[] args) +     { -A AA =NewA (); + aa.f (); A          at     } -}
In the code above:a aa = New A (); This line of code is the process of creating an object (instantiation of the Class).    a AA: Declares the object's reference variable, which is allocated in stack memory, which stores a memory address that points to the execution A chunk of memory allocated in the heap after new A ().
new A (): Creates an object that is dynamically allocated in heap memory. such as: Note: 1. New objects are allocated in the heap2. Local variables, such as object reference variables, are allocated in the stack       3. Static variables (variables modified by static) and string constants are stored in the data segtment
4. Storing code in code Segment



From for notes (Wiz)



Java: Class and Object concepts

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.