Java static variables and static functions

Source: Internet
Author: User
Tags variable scope

% classes with static methods are usually (though not necessarily) intended to be initialized.

When calling a static method in a class with a static method, use the class name directly. The method name is available. For example, Math.sin ();

static variables and static functions in Java

Static variables:
In Java, when a class is declared, it does not actually apply and generate memory space, just like a practical idea, but it does not actually implement the idea. A class can only request and generate a class-sized memory space in memory when it creates an object of a class with this data type, which is a backup of member variables and member functions. So at the time of class declaration, it is not possible to initialize the member variables, only objects created with the class can be initialized by the object to the variables, so, the resulting static variables!!! It is common to all class objects and has the same value for each class object. The keyword for a static variable is static, and its syntax is:

static< data type >< static variable name >=< initialize static variable >;

A static variable is initialized at the time of declaration and can only be initialized once, it has its own variable scope, but has a global life cycle and can be called at any time. When it declares, it retains a copy of the value from the beginning in memory. You can use the class name to tune a static member variable before the object of the class is created, and its syntax is:
< class name >.< static member variable >;

Static functions:
Since there are static variables there will be static functions, static functions only access static variables, other non-static functions are not allowed, but in a static member function can declare its own variables, it is also a function has other functions of the characteristics! The declaration of a static function and the declaration of a static variable are very poor:

static< data type >< static function name > (table of variables);

Like static variables, static functions exist in memory at the time of declaration (even if no object is created, which is not the same as other non-static functions), and also has a global life cycle.

Here is an example of a static function and a static variable:
Import java.io.*; Import IO Stream
public class Text_static
{
int iNum1;
static int iNum2;
text_static (int inum1,int iNum2)
{
THIS.INUM1=INUM1;
this.inum2=inum2;
this.inum1++;
this.inum2++;
}
public void Display1 (String str)
{
System.out.println (str+ ". inum1=" +INUM1);
}
Static functions can only access static variables
public static void Display2 (String str)
{
System.out.println (str+ ". inum2=" +inum2);
}
public static void Main (string[] args)
{
Text_static obj1=new text_static (n);
Obj1.display1 ("Obj1"); Output: inum1=2
Obj1.display2 ("Obj1"); Output: inum2=3
Text_static obj2=new text_static (10,20);
System.out.println ();
Obj2.display1 ("Obj2"); Output: inum1=11
Obj2.display2 ("Obj2"); Output: inum2=21
Obj1.display1 ("Obj1"); Output: inum1=2
Obj1.display2 ("Obj1"); Output: inum2=21
}
}

It is common for Java static members to run because Java does not have the concept of global functions and variables in C + +, but instead uses static members to implement the functions of global members in C + +.

The main difference between Java and C + + is that Java is completely object-oriented and is completely encapsulated by a class that does not exist as a global member in C + + and is replaced by static members in the class.

Java static variables and static functions

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.