A simple example shows the relationship between java packet class and variables. javapacket

Source: Internet
Author: User

A simple example shows the relationship between java packet class and variables. javapacket

1. PACKET is a folder, and the CLASS in the package accesses each other as a file.

 

2. The class is equivalent to a DELPHI calss. static functions can only access static functions.

Package Mainpack;

Public class Mainprg {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Human aman = new human ();
Human bman = new human ();
Aman. CreateNewMan ("James", 21, true );
Bman. GetAman ();
Showstr (bman. name );
}

Private static void showstr (String str ){
System. out. print (str );
}

}

3. static variables are equivalent to a public access space. New instances can still access data of other instances.

Package Mainpack;

Public class human {
Static String name = null;
Static int age = 0;
Static boolean sex = true;

Public void CreateNewMan (String nn, int aa, boolean ss ){
Name = nn;
Age = aa;
Sex = ss;
}

Public human GetAman (){
Human hh = new human ();
Hh. name = name;
Hh. age = age;
Hh. sex = sex;
Return hh;
}

}

 

The result shows michael Jacob.

 

The variable is removed from static and the display is null.

 


In java, what is the higher priority of local variables when they have the same name? It is better to give a simple example,

Public class test (){
Public static void main (string [] args ){
Int I = 3;
System. out. println (I );
}
Public void face (){
For (int I = 0; I <5; I ++ ){
System. out. println (I );
}
}
}

The result is 1 to 5. When two variables are named again, the priority of local variables is higher...

What is the relationship between classes and classes in java?

External class, internal class, Anonymous class, external class is the class A {} class B {}, internal class: class A {class B {}}, B can directly use the member variables of the class containing it. The internal class cannot be called by classes other than the inclusion class; there is also an anonymous class, the internal class classA {new {}} also omits the definition class name to directly define the method and use. This applies to code that is relatively simple and this class does not need to be called by other 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.