JAVA-classes, objects, and packages, java objects

Source: Internet
Author: User

JAVA-classes, objects, and packages, java objects

The most important feature of Java programming language is Object-Oriented Programming (OOP ).

A class is the description of an object, and an object is an instance of a class. An object consists of attributes and behaviors. An Attribute stone object has characteristics, and behavior is an action that an object can perform. Each property of an object is represented as a member variable in the class. Every action of an object becomes a method in the class.

In Java, the process becomes a method. Methods in Java appear in the class. The process in structured language usually appears globally, so that they can be called anywhere.

Classes in Java are declared with keywords class. Java source code files can only contain-public (public) classes. The file name must be the same as that of the public class, and the file extension must be ". java ".

The property of the object becomes a member variable in the relevant class. A member variable in the class is composed of the following parts:

■ Access modifier. It can be public, private, or protected. If the access modifier is omitted, the default access modifier is used.

■ Data type.

■ Member variable name. The member variable name must be a valid identifier and end with a semicolon.

For example:

Public class Emloyee {

Public String name; // name

Public String address; // mailing address

Public int number; // employee ID

Public int ssn; // The Social Security number.

Public double salary; // employee's salary

}

 

The behavior of an object becomes a method in the relevant class. A Method in a class is typically composed of the following parts:

■ Access modifier.

■ Return value.

■ Method name, which must be a valid identifier.

■ Parameter list, which appears in parentheses.

For example:

Public class Emloyee {

Public String name; // name

Public String address; // mailing address

Public int number; // employee ID

Public int ssn; // The Social Security number.

Public double salary; // employee's salary

// Mailing address Method

Public void mailCheck (){

System. out. println ("mail a check to" + name + ", address: \ n" + address );

}

// Wage Algorithm

Public void computerPay (){

Return salary/52;

}

}

 

In Java, each class belongs to one package. The package has two basic purposes:

■ Provides an organizational class mechanism.

■ Provide a namespace for the classes in the package.

 

When a class is placed in a package, two main results are displayed: ① The package name is a part of the class name. ② The package name must be consistent with the directory where the relevant bytecode is stored.

The new keyword is used to instantiate an object. The New operator returns a reference to the newly created object. The object is in memory until there is no reference pointing to it. In this case, the object meets the garbage collection condition.

The vertex operator is used to reference member variables and methods used to access objects together.

Each object has a reference to itself. this reference is a this reference.

A package is a mechanism for organizing and managing classes in Java. It can also prevent class naming conflicts.

You can use the keyword import to import classes in other packages.

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.