Java Objects and classes

Source: Internet
Author: User

The following is referenced from http://wiki.jikexueyuan.com/project/java/object-classes.html:

I. Overview

Java is an object-oriented language. As a language with object-oriented features, Java includes the following basic concepts:

    • Polymorphism
    • Inheritance of
    • Encapsulation of
    • of abstraction
    • Class
    • Object
    • Instance
    • Message parsing

What are classes and objects:

    • Object : The object has state and behavior. such as the real world: cars, dogs, humans and so on. All of these objects have state and behavior.
    • class : A class can be defined as a template that describes the behavior of the object's supporting types, the state of the templates, and the blueprint.

Second, the object in Java

If you consider a dog, then its state is: name, variety, color, its behavior is barking, wagging the tail, run.

If you compare objects in the software with objects in the real world, you will find that they have many similar characteristics. Objects in the software also have state and behavior. The state of the software is stored in a file, and its behavior is represented by methods.

Therefore, in the process of software development, the method controls the internal state of the object and the communication between objects and objects is accomplished by means of methods.

Third, classes in Java

A class is a blueprint created by a separate object.

An example of a class is given below:

 Public class dog{   String breed;    int Age ;   String color;    void Barking () {   }   void  hungry () {   }   void  sleeping (){   }}

A class can include the following variable types:

    • local variable : A variable defined in a method, constructor, or range becomes a local variable. Variables will be generated and developed within the method, and then when the method ends the variable will break.
    • instance variable: an instance variable is a variable within a class, but outside of a method. These variables are manifested when the class is loaded. Instance variables can be accessed from any method, constructor, and region of a particular class.
    • class variable: A class variable is a variable declared in a class that is outside any method and has a static keyword.

Classes can be accessed by any number of methods to access the values of different kinds of methods. In the example above, barking() hungry() and is the sleeping() method.

Four, the constructor

Each class has a constructor. If you do not write a constructor for a class individually, the Java compiler will create a default constructor for the class.

Whenever a new object is created, at least one of the constructors will be called. One of the main tenets of the constructor is that they must have the same name as the class. A class can have more than one constructor. And there is no return value.

An example of a constructor is given below:

 Public class puppy{   public  Puppy () {   }   public  Puppy (String name) {    }}

Java also supports singleton when it is necessary to create only one instance of a class.

V. Creation of an Object

As mentioned earlier, the class provides a blueprint for the object. So basically, an object is created from a class. In Java, new keywords are used to create new objects. The steps to create the class are as follows:

    • Declaration: A variable declaration can declare the type of object it represents.
    • Instantiation: "New" keywords are used to create objects.
    • Initialize: the "new" keyword is associated with the enablement of a constructor, which initializes the new object.

Here is an example of a creative object:

 Public classpuppy{ PublicPuppy (String name) {//This constructor has one parameter, name.System.out.println ("Passed Name is:" +name); }    Public Static voidMain (String []args) {//following statement would create an object MypuppyPuppy Mypuppy =NewPuppy ("Tommy" ); }}//if you compile and run the above program, the following results will be output:Passed Name Is:tommy

VI. accessing entity variables and methods

Entity variables and methods are accessed through the creation of objects. The path that is fully valid in order to access an entity variable should be as follows:

/** *new  Constructor (); /*  */objectreference.variablename; /*  */Objectreference.methodname ();

Example:

This example explains how to access the entity variables and methods of a class:

 Public classpuppy{intPuppyage;  PublicPuppy (String name) {//This constructor has one parameter, name.System.out.println ("Passed Name is:" +name); }    Public voidSetage (intAge ) {Puppyage=Age ; }    Public intGetage () {System.out.println ("Puppy's age is:" +puppyage); returnPuppyage; }    Public Static voidMain (String []args) {/*Object Creation*/Puppy mypuppy=NewPuppy ("Tommy" ); /*Call class method to set puppy ' s age*/Mypuppy.setage (2 ); /*Call Another class method to get puppy ' s age*/Mypuppy.getage (); /*You can access instance variable as follows as well*/System.out.println ("Variable Value:" +mypuppy.puppyage); }}//if you compile and run the above program, the following results will be generated:Passed Name is:tommypuppy' s age is:2variable value:2

Vii. source File Declaration rules

These rules are important when declaring classes in the source file, entering and packaging the syntax.

    • There can be only one public class in each source file.
    • A source file can have many non-public classes.
    • The name of the public class must be the name of the source file as well .java as the suffix. For example: The name of the class is public class Employee{} , then the source file should be Employee.java .
    • If a class is defined in a package, the declaration of the package must be the first declaration of the source file.
    • If the input claims appear then they must be written between the encapsulating Declaration and the class declaration. If there is no encapsulation declaration then the input declaration must be in the first line of the source file.
    • The input and packaging declarations imply the existence of all classes in the source file. It is difficult to differentiate between input and encapsulation declarations for different classes in the source file.
      Classes have different access levels and have many different classes, abstract classes, final classes, and so on.
      In addition to the types of classes mentioned above, Java also has special classes such as inner and anonymous classes.

Viii. Java Packages

In short, it is a way of classifying classes and interfaces. When you develop a program in Java, hundreds of classes and interfaces are written, so it is necessary to classify these classes and also make the problem easier.

Nine, import syntax

In Java, if you give a fully qualified name that includes the names of the wrappers and classes, the compiler can easily navigate to the source class and source code. Import syntax is a way to find the appropriate location for a particular class for the compiler.

For example, the following line of statements would require the compiler to load java_installation/java/io all the available classes under the path:

import java.io.*;

Ang Setting a simple case

The following will create two classes: Employee and Employeetest.

This employee class includes four entity variable names (name), age, Position (designation), and salary (salary). This class has a constructor that is deterministic and requires parameters.

ImportJava.io.*; Public classemployee{String name; intAge ;   String designation; Doublesalary; //This is the constructor of the class Employee    PublicEmployee (String name) { This. Name =name; }   //Assign The age of the Employee to the variable.    Public voidEmpage (intempage) { Age=Empage; }   /*Assign the designation to the variable designation.*/    Public voidempdesignation (String empdesig) {designation=Empdesig; }   /*Assign The salary to the variable salary.*/    Public voidEmpsalary (Doubleempsalary) {Salary=empsalary; }   /*Print the Employee details*/    Public voidPrintemployee () {System.out.println ("Name:" +name); System.out.println ("Age:" +Age ); System.out.println ("Designation:" +designation); System.out.println ("Salary:" +salary); }}

The main function and class instantiation should be established in order to run this employee class. The classes are created for these tasks, respectively.

The following is the Employeetest class, which creates instances of two employee classes and invokes methods on each object to assign values to each variable.

ImportJava.io.*; Public classemployeetest{ Public Static voidMain (String args[]) {/*Create objects using constructor*/Employee Empone=NewEmployee ("James Smith"); Employee Emptwo=NewEmployee ("Mary Anne"); //invoking methods for each object createdEmpone.empage (26); Empone.empdesignation ("Senior software Engineer"); Empone.empsalary (1000);      Empone.printemployee (); Emptwo.empage (21st); Emptwo.empdesignation ("Software Engineer"); Emptwo.empsalary (500);   Emptwo.printemployee (); }}

Compiling two classes and then running Employeetest, you will see the following result:

C:> Javac Employee. JavaC:> VI employeetest. JavaC:> JavaC employeetest. JavaC:> java employeetestname: James  smithage:designation: Senior  software engineersalary:1000.0Name: Mary  anneage:  Designation: Software  engineersalary:500.0

Test Project: Https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test2

Java Objects and classes

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.