Deep understanding of Java objects and class _java

Source: Internet
Author: User
Tags class definition

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

• polymorphism
• Inheritance
• Encapsulation
• Abstract
• Class
• objects
• examples
• methods
• Message Resolution

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

• Object: An object is an instance of a class, with state and behavior. For example, a dog is an object whose state is: color, name, breed; behavior: WAG tail, bark, eat, etc.

• Class: A class is a template that describes the behavior and state of a class of objects.

Objects in Java

Now let's delve into 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 has: name, breed, color, behavior has: call, WAG tail and run.

Comparisons between realistic objects and software objects are very similar.

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

In software development, methods are used to change the internal state of the object, and the mutual invocation of the object is accomplished by means.

Classes in Java

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

Understand the definition of classes in Java by following a simple class:

public class dog{ 
  String breed; 
  int age; 
  String color; 
  void Barking () { 
  } 
   
  void Hungry () { 
  } 
   
  void Sleeping () { 
  } 
}} 

A class can contain the following types of variables:

• Local variables: variables defined in a method, construct method, or statement block are called local variables. Variable declarations and initializations are in the method, and when the method is finished, the variable is automatically destroyed.

• Member variables: member variables are variables 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 statements blocks of specific classes in the class.

• Class variables: Class variables are also declared in the class, outside the method body, but must be declared as a static type.

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

Construction method

Each class has a construction 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 of the constructor methods is invoked when creating an object. The name of the constructor must be the same as the class, and a class can have multiple construction methods.

The following 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. The following three steps are required to create an object:

• Declaration: Declares an object, including the object name and object type.

• Instantiate: Use the keyword new to create an object.

• Initialize: When you create an object using new, the constructor initialization object is called.

The following is an example of creating an object:

public class puppy{public 
  Puppy (String name) { 
   //This constructor has only one argument: name 
   System.out.println ("Passed name is:" + name); 
  public static void Main (String []args) { 
   //The following statement creates a Puppy object 
   Puppy mypuppy = new Puppy ("Tommy"); 
  } 
 

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

Passed Name is:tommy

Accessing instance variables and methods

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

/* Instantiate Object *
/objectreference = new Constructor (); 
* * Access to the variable * *
objectreference.variablename; 
/* Access the method in the class *
/Objectreference.methodname (); 
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 argument: name 
   System.out.println ("Passed name is:" + name); 
  } 
 
  public void Setage (int age) { 
    puppyage = age; 
  } 
 
  public int getage () { 
    System.out.println ("Puppy ' s"): + puppyage); 
    return puppyage; 
  } 
 
  public static void Main (String []args) { 
   */* Create object */
   Puppy mypuppy = new Puppy ("Tommy"); 
   /* Through the method to set the age
   /Mypuppy.setage (2); 
   /* Call another method to get the age
   /Mypuppy.getage (); 
   /* You can also access member variables like this
   /System.out.println ("Variable Value:" + mypuppy.puppyage); 
  } 
 

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

Passed Name is:tommy
Puppy ' s Age is:2
Variable Value:2

source file Declaration Rules
in the last section of this section, we will learn the declaration rules for the source file. You should pay special attention to these rules when you define multiple classes in a source file, and there are import and package statements.

• 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, and the source file should be named Employee.java.

• If a class is defined in a package, then the package statement should be in the first line 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 at the front of the source file.

Import Statements and package statements are valid for all classes defined in the source file. In the same source file, a different package declaration cannot be given to different classes.

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

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

Java Packages

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

Import statement

In Java, if you give a full qualified name, including the package name, the class name, then 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.*;

A simple example

In this example, we create two classes: Employee and Employeetest.

First open the text editor and paste the following code in. Note Save the file as a Employee.java.

The employee class has four member variables: name, age, designation, and salary. This class explicitly declares a construction method with only one argument.

Import java.io.*; 
public class employee{ 
  String name; 
  int age; 
  String designation; 
  Double salary; 
  Constructor public 
  employee (String name) { 
   this.name = name; 
  } 
  of the Employee class Sets the value of age of public 
  void empage (int empage) {Age 
   = empage; 
  } 
  /* Set designation value of
  /* public void Empdesignation (String empdesig) { 
   designation = Empdesig; 
  } 
  /* Set salary value of
  /* public void empsalary (double empsalary) { 
   salary = empsalary; 
  } 
  /* Print information/* Public
  void Printemployee () { 
   System.out.println ("Name:" + name); 
   System.out.println ("Age:" + age); 
   SYSTEM.OUT.PRINTLN ("designation:" + designation); 
   System.out.println ("Salary:" + Salary); 
  } 

Programs are executed from the main method. In order to run this program, you must include the main method and create an instance object.

The Employeetest class, which instantiates instances of 2 employee classes and invokes the values of the method settings variable, is given below.

Save the following code in the Employeetest.java file.

Import java.io.*; 
public class employeetest{public 
 
  static void Main (String args[]) {/ 
   * Create two objects using the constructor */
   Employee empone = new EMP Loyee ("James Smith"); 
   Employee Emptwo = new Employee ("Mary Anne"); 
 
   Call the Member Method 
   Empone.empage (n) of the two objects; 
   Empone.empdesignation ("Senior Software Engineer"); 
   Empone.empsalary (1000); 
   Empone.printemployee (); 
 
   Emptwo.empage (); 
   Emptwo.empdesignation ("Software Engineer"); 
   Emptwo.empsalary (); 
   Emptwo.printemployee (); 
  } 
 

Compile these two files and run the Employeetest class, you can see the following results:

C:> javac Employee.java 
c:> vi employeetest.java 
c:> javac Employeetest.java 
c:> Java Employee Test 
name:james Smith 
age:26
designation:senior Software Engineer 
salary:1000.0
name:mary Anne 
age:21
Designation:software Engineer 
salary:500.0 

The above in-depth understanding of Java objects and classes is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.