Classes and objects in Java

Source: Internet
Author: User

Recently to find an internship just remembered to turn out a long time did not touch Java, hope is not too late, plan to smoke a few days to read "Head first Java", this book translation is good recommended to everyone ha.

As for the difference between classes and objects, a class is actually an abstraction of the same class of objects, and the object is a concrete instance of a class. In fact, like Marx inside the abstract and concrete there is wood, I believe that the understanding of learning is still very easy.

For example, you can define a class that is Gaofu, and constructed is an object. 23333

All say that Java is an object-oriented language, so what exactly needs to be able to use the object??

Answer: Requires two classes: 1. A class that we define that is to be manipulated to the object (for example, Dog, AlarmClock)

2. The other is the class used to test the class. The test class is the class where the main method resides, and you need to establish and access the object of the class being tested in the Main method .

Here's an example of how to use an object:

1. Write a class:

1 classDog {2     //instance face changing (instance varible)3     intsize;4 String breed;5 String name;6     //Method7     voidbark () {8System.out.println ("ruff! ruff! ");9     }Ten}

2. Write the test class (that is, the class where the Main method resides)

1  Public class dogtestdrive {2      Public Static void Main (string[] args) {3         // Test Code 4     }5 }

3. Creating objects and accessing objects ' variables and methods in a test class

 1  public  class   dogtestdrive { public  static  void   main (string[] args) { 3  //  test code  4  Dog d = new   Dog ();  5  d.size = 40;  6   D.bark ();  7   8  }

So there has to be a change here, JAva is an object-oriented programming language, so long as you stay in main and that's outside of the object, main works on the class of the test.

There are two main uses of main () in Java:

1. Test the Real class

2. Launch your Java application

The essence of a real Java program is that the object interacts with the object (the objects call each other method), so get out of main now!!

Classes and objects in Java

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.