Static usage in java

Source: Internet
Author: User
Tags dota iqiyi

Static usage in java

We can create multiple objects of this Class Based on a class. Each object has its own Members. Sometimes, we want all the members of this class to share one member. Static can be used for modification.

Static in Java can be used as modifiers to modify variables, methods, and code blocks.

1. Static Method

Variables modified by static can become static variables and static members or class members. It belongs to the whole class, not to a certain object, and is shared by all objects of the class. Class variables initialize and allocate space during class loading, and are initialized only once until the class is detached.

Static members can be accessed directly by class names or objects. Any object in the program modifies static variables. Other Objects see the modified values.

Public class Test {static String play = "football"; public static void main (String [] args) {System. out. println ("Access static variables through classes," + play); Test test = new Test (); System. out. println ("Access static variables through objects," + test. play); test. play = "basketball"; System. out. println ("Access static variables through objects," + test. play );}}


2. Static Method

Static methods are called static methods or class methods. The commonly used main method is static methods.

Static variables of the same type can be called directly in static methods, but non-static variables cannot be called directly. Non-static variables can be accessed after an object is created. Non-static methods cannot be called directly in static methods, you can access non-static methods through objects.

Public class Test {String game = "DOTA"; static String play = "football"; public static void main (String [] args) {print ();} public static void print () {System. out. println ("Access static variables," + play); // System. out. println ("access to non-static variables," + game); // The static method cannot directly access the non-static variable Test = new test (); System. out. println ("access non-static variables," + test. game); // show (); // The static method cannot directly call the non-static method test. show () ;}// in normal methods, you can directly access non-static variables of the same type and static variables public void show () {System. out. println ("Access static variables," + play); System. out. println ("access to non-static variables," + game); print (); // normal methods can directly call static methods }}

 

3. Code Block

Use static to modify an independent code block in the class, which is called a static code block.

The static initialization block is executed only when the class is loaded and only once. The static code block has no name and cannot display the call. It can only be called when the class is loaded.

Static initialization blocks can only assign values to static variables, but cannot initialize common member variables.

Public class Test {String film; String game; static String play; public static void main (String [] args) {Test test = new Test (); System. out. println ("film =" + test. film); System. out. println ("game =" + test. game); System. out. println ("play =" + play); Test test2 = new Test ();} public Test () {film = "iQiYi"; System. out. println ("Assign film via constructor") ;}{ game = "DOTA"; System. out. println ("assign a value to game by initializing blocks");} static {play = "football"; // game = "DOTA"; // The value of System cannot be assigned. out. println ("assign a value to play through static initialization block ");}}


Output result:

Assign a value to play through static initialization Blocks
Assign a value to game through initialization Block
Assign a value to film using the constructor
Film = iQiYi
Game = DOTA
Play = football
Assign a value to game through initialization Block
Assign a value to film using the constructor

 

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.