A brief analysis of Java static keyword usage _java

Source: Internet
Author: User
Tags static class

This article analyzes the static usage in Java, mainly five aspects: static member variable, static method, static block, static internal class, static guide package.

First, it's a table. The difference between a static and a Non-static object:

Static objects Non-static objects
Belong Class together have Each instance of the class has an independent
Memory allocation Fixed on the memory space Subordinate class assignment
Allocation Space Order Priority allocation of static object space Prioritize static object space, as well as initialization

1 static variables, static methods, static blocks

Static objects, static methods are decorated with the static keyword on the original object and method, which means that the class can call these directly without having to instantiate it and then call it. The benefits are:

1--static object data is globally unique, change all changes, can guarantee the uniqueness of the data

2--reference is convenient, directly using the class name. Method (properties), no more get and set

Of course, static methods (variables, blocks) also have corresponding restrictions:

1--cannot invoke other non-static methods (only static methods can be called)

2--can only access static data

3--cannot refer to this or super in any way

Sample code:

public class Staticexercise {public
static String sky= "Jeyson";
public static string Getmysky (String sky) {return
sky;
}
static {
System.out.println ("This is the content of the static Block-");
System.out.println ("Static Block:" +getmysky (Sky));
}
/** * * * * *
static Object benefits:
* 1--static object data is globally unique, change all, can guarantee data unique.
* 2--Reference is convenient, direct class name. method is OK, do not use set and get
* * * * * * allocation space order:
* First allocate static object space redistribution Non-static. So is the initialization order
. * ***static final decorated member variables and member methods can be understood as global variables (there is no global variable concept in Java) * * * * * *
static objects, methods, block restrictions:
* 1-- Only other static methods can be invoked
* 2--can only access static data
* 3--cannot reference this or super
* static object Test in any way * *
@Test
Public void TestStatic1 () {
System.out.println (sky);
System.out.println ("-----------------");
System.out.println (Getmysky (Sky));
System.out.println ("-----------------");
System.out.println (Statictest.getsms ());
}

2 Static inner class

When a class is closely related to another class (The outer Class), it is generally used only when the outer class is used, and the class can be treated as a static inner class. This can be directly generated as the class is loaded. No longer can be instantiated.

Use method: To be loaded with the class, to invoke the direct outer class. Internal classes. Static Objects (methods)

Sample code:

Class statictest{public
static String sms= "to My Sky";
public static String Getsms () {return
SMS;
}
/**
* Static internal class
* * * * * * Use scene:
* Internal classes and external classes are closely related to the general use of external classes will only use this kind of * * * * * with the
class is loaded, direct external class. Internal classes. Static Objects (methods)
*
/Static class innerstatic{public
static final String mysky= ' my final SKY ';
}
} 
@Test public
void TestStatic2 () {
System.out.println (StaticTest.InnerStatic.MYSKY);

2 Static Guide Package

If you want to use a static method with a class that is not under the same package as the current class, you can use a static guide package. This way the static method or variable you want to use is equivalent to being visible directly in the class, without having to call the class name. Static variables.

How to use:

Import static package name. class name. Static member variable (method)

Benefits:

The static variables and methods that are imported are directly visible in the class, directly using the

Harm:

Reduces the readability of the code to some extent

code example:

public class Mystaticimporttest {public
static final int number=55;
public static void Getmywords () {
System.out.println ("Hello, this is a test for static guide package");
} 

Import static com.generic.MyStaticImportTest.number;
Import static com.generic.MyStaticImportTest.getMyWords;
/**
* @Description:
* static/Public
class Staticexercise {
/**
* Static Guide Package
* * * * * * * * * * * * use:
* Direct static import of the corresponding variables and methods: (Import static package name. class name. Static member variable (method)
* import static com.generic.MyStaticImportTest.number;
* Import static com.generic.MyStaticImportTest.getMyWords;
* * * * Benefits:
* The imported static methods and variables are directly visible in this class, no need to give the class name * * * * * * *
The disadvantage of some degree of reduction in the readability of the code * *
@Test public
Void TestStatic3 () {
//static import can be directly used with
int mynumber=number;
Getmywords ();
}

The above is a small series to introduce you to the analysis of Java static keyword usage, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.