Definition and instantiation of the 1.Java class

Source: Internet
Author: User

class must be defined before it can be used. The class is the template that creates the object, and the object that is created is also called the instantiation of the class.

Here's a simple example to understand the definition of a class in Java:

  1. Public class Dog{
  2. String name;
  3. int age;
  4. void Bark(){ //bark
  5. System. Out. println("bark, don't come Over");
  6. }
  7. void hungry(){ //hunger
  8. System. Out. println("Master, I'm Hungry");
  9. }
  10. }

Description of the example:

    • Public is a modifier of a class that indicates that the class is a common class and can be accessed by other classes. Modifiers are explained in the next section.
    • Class is a keyword that defines a class.
    • Dog is the class name.
    • Name, age is a member variable of a class, also called an attribute; bark (), hungry () is a function in a class, also called a method.


A class can contain the following types of variables:

    • Local variables: Variables defined in a method or block of statements are called local variables. Both the Declaration and initialization of variables are in the method, and the variables are automatically destroyed when the method ends.
    • Member variables: Member variables are variables that are defined in the class, outside the method body. This variable is instantiated (allocating memory) when the object is created. Member variables can be accessed by methods in the class and statements of a particular class.
    • Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static types. Static is also a modifier, which is explained in the next section.

Construction method

A method that is automatically executed during a class instantiation is called a construction method, and it does not require you to invoke it manually. The construction method can do some initialization work during the instantiation of the class.

The name of the construction method must be the same as the name of the class, and there is no return value.

Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for that class.

Here is an example of a construction method:

  1. Public class Dog{
  2. String name;
  3. int age;
  4. //constructor method, no return value
  5. Dog(String name1, int age1){
  6. Name = name1;
  7. Age = Age1;
  8. System. Out. println("Thank the Master for adopting Me");
  9. }
  10. //Normal method, must have a return value
  11. void Bark(){
  12. System. Out. println("bark, don't come Over");
  13. }
  14. void hungry(){
  15. System. Out. println("Master, I'm Hungry");
  16. }
  17. public static void main(String arg[]){
  18. //The arguments passed when creating the object should correspond to the constructor method parameter list
  19. dog Mydog = new dog("Flower", 3);
  20. }
  21. }

Operation Result:
Thank you, Master, for adopting me.

Description

    • The constructor method cannot be displayed for invocation.
    • The constructor method cannot have a return value because there is no variable to receive the return value.

Creating objects

An object is an instance of a class, and the process of creating an object is also called an instantiation of the class. Objects are created with a class as a template.

In Java, using the new keyword to create an object typically has the following three steps:

    • Declaration: Declares an object, including the object name and object type.
    • Instantiation: Use the keyword new to create an object.
    • Initialize: When you create an object with new, the constructor method is called to initialize the object.


For example:

    1. Dog Mydog; //Declare an object
    2. Mydog = new Dog("Flower", 3); //instantiation

It can also be initialized at the same time as the declaration:

    1. Dog Mydog = new dog("Flower", 3);

Accessing member variables and methods

Access member variables and member methods through the objects you have created, such as:

    1. Instantiation of
    2. Dog Mydog = new dog("Flower", 3);
    3. Accessing member variables by point number
    4. Mydog. Name;
    5. Accessing member methods by point number
    6. Mydog. Bark();


The following example shows how to access member variables and methods:

  1. Public class Dog{
  2. String name;
  3. int age;
  4. Dog(String name1, int age1){
  5. Name = name1;
  6. Age = Age1;
  7. System. Out. println("Thank the Master for adopting Me");
  8. }
  9. void Bark(){
  10. System. Out. println("bark, don't come Over");
  11. }
  12. void hungry(){
  13. System. Out. println("Master, I'm Hungry");
  14. }
  15. public static void main(String arg[]){
  16. dog Mydog = new dog("Flower", 3);
  17. //Access member variables
  18. String Name = Mydog. Name;
  19. int Age = Mydog. Age;
  20. System. Out. println("I am a puppy, my name is" + name + ", i" + age + "old");
  21. //Access Method
  22. Mydog. Bark();
  23. Mydog. Hungry();
  24. }
  25. }

Operation Result:
Thank you, Master, for adopting me.
I'm a puppy, my name is flower, I'm 3 years old.
Bark, don't come over.
Master, I'm hungry.

Definition and instantiation of the 1.Java class

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.