Java Objects and classes

Source: Internet
Author: User
Tags class definition

Java Objects and classes

Java as an object-oriented language. The following basic concepts are supported:

    • Polymorphic
    • Inherited
    • Packaging
    • Abstract
    • Class
    • Object
    • Instance
    • Method
    • Overload

In this section we focus on the concepts of objects and classes.

  • Object: An object is an instance of a class ( objects are not looking for a girlfriend ), stateful and behavioral. For example, a dog is an object whose state is: color, name, variety, behavior: wagging the tail, barking, eating, etc.
  • class : A class is a template that describes the behavior and state of a class of objects.
  • Objects in Java

    Now let's take a closer look at what an object is. Look around the real world, you will find a lot of objects around, cars, dogs, people and so on. All of these objects have their own state and behavior.

    Take a dog for example, its state is: name, variety, color, behavior has: call, WAG tail and run.

    Compare real objects with software objects, which are very similar.

    Software objects also have states and behaviors. The state of the software object is the attribute, and the behavior is manifested by the method.

    In software development, the internal state of the method operation object is changed, and the mutual invocation of the object is done by means of the method.

    Classes in Java

    Class can be seen as a template for creating Java objects.

    Use the following simple class to understand the definition of a class in Java:

    Public Class Dog{ String Breed;Int Age;String Colorvoid barking ({ } void hungry ({ } void sleeping< Span class= "Hl-brackets" > () { } }

    A class can contain the following types of variables:

      • Local Variables : variables defined in a method, construction 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 when the object is created. Member variables can be accessed by methods, construction methods, and statement blocks of specific classes within a class.
      • Class variables : Class variables are also declared in the class, outside the method body, but must be declared as static types.

    A class can have multiple methods, in the example above: Barking (), hungry (), and sleeping () are all methods of the dog class.

    Construction method

    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 the class.

    At least one constructor method is called when an object is created. The name of the constructed method must be the same as the class, and a class can have more than one constructor method.

    Here is an example of a construction method:

    Public Class Puppy{public Puppy(){ } public Puppy(String name){ // This constructor has only one parameter: name } } Creating objects

    Objects are created from a class. In Java, use the keyword new to create a new object. Creating an object requires 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.

    Here is an example of creating an object:

    Public Class Puppy{ Public Puppy(String Name){ //This constructor has only one parameter: name System.Out.println("The puppy's name is:"+Name );} Public Static void Main(String []args< Span class= "Hl-brackets" >) { // puppy mypuppy = new Puppy< Span class= "Hl-brackets" > ( "tommy " ) } } /span>

    Compiling and running the above program will print out the following results:

    The puppy's name is  : Tommy 
    Accessing instance variables and methods

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

    /*Instantiating an Object*/ objectreference = new Constructor(); / * access the variables in the */ objectreference. VariableName; / * access the methods in the class */ objectreference. MethodName(); Instance

    The following example shows how to access an instance variable and invoke a member method:

    Public Class Puppy{ Int Puppyage;Public Puppy(String Name){ //This constructor has only one parameter: name System.Out.println("The puppy's name is:"+Name );} Public void Setage( Int Age ){ Puppyage=Age;} Public Int Getage( ){ System.Out.println("The age of the puppy is:"+Puppyage );Return Puppyage;} Public Static void Main(String []Args){ /*Creating objects*/ Puppy Mypuppy=New Puppy( "Tommy" );/*Use the method to set age*/ Mypuppy.Setage( 2 );/*Call another method to get the age*/ Mypuppy.getage ( ; /* You can also access member variables like the following */ system. Out. Println ( " variable values: " + mypuppy.< Span class= "Hl-identifier" >puppyage } }

    Compile and run the above program to produce the following results:

    The puppy's name is  : Tommy puppy 's age :2 variable value :2       
    Source file claim rules

    In the final part of this section, we will learn the claim rules for the source file. When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.

      • There can be only one public class in a source file
      • A source file can have multiple non-public classes
      • The name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should be named Employee.java.
      • If a class is defined in a package, then the packages statement should be in the first row of the source file.
      • If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.
      • The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.

    The class has several access levels, and the classes are divided into different types: abstract and final classes, and so on. These will be described in the Access Control section.

    In addition to some of the types mentioned above, Java also has some special classes, such as: Inner class, anonymous class.

    Java Package

    Packages are primarily used to classify classes and interfaces. When developing Java programs, you might write hundreds or thousands of classes, so it is necessary to categorize classes and interfaces.

    Import statement

    In Java, if you give a full qualified name, including the package name, the class name, the Java compiler can easily navigate to the source code or class. The import statement is used to provide a reasonable path so that the compiler can find a class.

    For example, the following command line will command the compiler to load all classes under the Java_installation/java/io path

    Import java.  Io. *;   

Java Objects and classes

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.