In Java, the concept of a package, constants, static members, inheritance

Source: Internet
Author: User

New Package : Upper left corner-new-Package

Naming conventions (usually from large to small, easy to integrate and not easy to conflict) For example: Com.itnba.maya.test

The package must be in the top row, and no more code can be

Using packages:

Shortcut: Use a variable name in the package alt+/

Import package name. class name;       Reference only one of the import package names in the package. *;     Refer to all examples in the package   import com.itnba.maya.*;

Constants : Final (There are no true constants in Java.)   Only the final type of variable can be used as represented. One definition, multiple use, cannot be changed)

Public final String ssss= "Hello World";     Constant names are all capitalized

The assignment of a constant can only be assigned at the time of the constructor or definition, and can only be assigned once

static member static corresponds to static, "instance"

private static String zhuren;//private static type variable;

  

Static members include: Static variables and static methods (functions)

Static variables and methods are shared by all objects (belonging to the Class) static methods can be invoked with the class name advantages: Save space

Example: Suppose we set up a class with the following code

Package Com.itnba.maya.ceshi;public class Cat {private String mingzi= "Xiao Huan";            Member variable private static String zhuren= "Harry";   Static member variable private String daxiao= "very small";            Member variable public static String Getzhuren () {      //get get return zhuren;} public static void Setzhuren (String zhuren) {      //set change cat.zhuren = Zhuren;}}

Then we call

Package Com.itnba.maya.ceshi;public class Jiao {public static void main (string[] args) {//TODO auto-generated method stub cat Mao =new Cat ( ); Cat mao1=new Cat (); Cat mao2=new Cat ();    System.out.println (Mao.getzhuren ());   The default value is    mao1.setzhuren ("Zhang San"); System.out.println (Mao1.getzhuren ());   Mao2.setzhuren ("John Doe") after assigning "Zhang San";           System.out.println (Mao2.getzhuren ());           System.out.println (Mao.getzhuren ()) After assigning "John Doe";            Finally, we output the default value again}}

The output is as follows

Why does the last output default become John Doe? The default value changes the properties of static members and static methods as each change changes: Static and static methods are shared by all objects

Static variables when storing data must be careful to prevent sharing conflicts.

Static methods can only manipulate static variables and static methods of the current class, cannot manipulate instance variables and instance methods, and call static methods in instance methods

In Java, the concept of a package, constants, static members, inheritance

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.