Summary of static variables and methods in Java

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/haobo920/article/details/5921621

Summary of static variables and methods in Java

Everything in Java is an object

The definition of an object in a class is usually done in two steps:
1/A A; Defines a reference to Class A
2/a=new A ("10", "2563"); Actually set up object A, which is a pointing to a contiguous area in memory

That is, usually we say create a class, just point out the appearance and behavior of the class, unless you create an object of that class with new, otherwise you don't actually get anything. The data storage space is not formally generated until new is executed, and the appropriate method can be used.
But if we want to use only one storage area to hold a specific data-no matter how many objects we create, we don't even create the object at all. Or we need a special method that is not associated with any object of this class. That is, even if you do not create an object, you need a method that can be called. To meet these two requirements, you can use the static (static) keyword.

Once something is set to static, the data or method will not be associated with any object instances of that class. So although an object of that class has never been created, you can still invoke a static method, or access some static data. Instead of static methods or variables, you must know the exact object they are working on.

Here are examples and explanations of Java programming ideas:

For example, the following code can generate a static data member and initialize it:

Class Statictest {
Static int i = 47;
}

Now, although we have made two Statictest objects, they still occupy only one storage space of statictest.i. Both of these objects share the same i. Please examine the following code:
Statictest st1 = new Statictest ();
Statictest st2 = new Statictest ();
At this point, both st1.i and st2.i have the same value of 47, because they refer to the same memory area.
There are two ways to reference a static variable. As shown above, it can be named by an object, such as st2.i. It can also be referenced directly by its class name, which is not feasible in non-static members (it is best to refer to the static variable as it emphasizes the "static" nature of the variable).
statictest.i++;
where the + + operator adds value to the variable. At this point, the value of either st1.i or ST2.I is 48.
Similar logic applies to static methods as well. You can refer to a static method as you would any other method by using an object, or a special syntax format "class name. Method ()". The definition of a static method is similar:
Class Staticfun {
static void Incr () {statictest.i++;}
}
It can be seen from this that the Staticfun method incr () adds value to the static data I. Through the object, a typical method called INCR () can be used:
Staticfun SF = new Staticfun ();
SF.INCR ();
Or, because INCR () is a static method, it can be called directly from its class:
STATICFUN.INCR ();
Although it is "static", it will explicitly change the way data is created (a member of a class and a non-static member of each object) as long as it is applied to a data member. If applied to a method, it is less dramatic. For a method, static is an important use to help us invoke that method without having to create an object. As you'll see later, this is critical-especially when defining a program to run the Entry method main ().
As with any other method, the static method can also create a named object of its own type. Therefore, the static method is often used as a "leader" to generate a series of "instances" of its own type.

Test examples:
Package moonsoft.test.testDataStructure;


public class Testdriver
{
Public Testdriver ()
{
}
public static void Main (String []args)
{
Testdriver td=new testdriver ();
String Month=td.monthname (3);//Calling static method from an object is not recommended for use
Month=testdriver.monthname (4);//Standard call to static method
String month1=td.monthname1 (3);//Call non-static method through an object if you need to access a non-statically methods, you can use
MONTH1=TESTDRIVER.MONTHNAME1 (4); Error is here
/* Do not call non-static method through object:
MONTH1=MONTHNAME1 (4);//error .....
* Error (14,23): Non-static method monthName1 (int) cannot be referenced from a static context*/
System.out.println (month+a);//static method call static variable
System.out.println (month1+a);//static method call static variable
System.out.println (MONTH+A1);
/* Static method call non-static variable
*error (17,30): non-static variable A1 cannot is referenced from a static context*/

}
public static String monthName (int month)//static method
{
Switch (month)
{
Case 1:return "Jan";
Case 2:return "Feb";
Case 3:return "Mar";
Case 4:return "APR";
Case 5:return ' may ';
Case 6:return "June";
Default:return "Others";

}
}
Public String monthName1 (int month)//not a static method
{
Switch (month)
{
Case 1:return "Jan";
Case 2:return "Feb";
Case 3:return "Mar";
Case 4:return "APR";
Case 5:return ' may ';
Case 6:return "June";
Default:return "Others";

}
}
public static int Add (int t)
{
int s=t;
s++;
return s;
}
public static Double A;
public double A1;

}

Summary of static variables and methods in Java

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.