Variables, methods, and static and non-static

Source: Internet
Author: User

variables
Types of variables:
1. Member variables
2. Local Variables
The difference between a member variable and a local variable:
Define the difference between locations
1. The member variable is defined outside the method, within the class.
2. Local variables are defined within the method.
difference in function:
1. Member variables are used to describe the public properties of a thing.
2. A local variable provides a variable for internal use of the method.
Differences in life cycle:
1. The member variable exists as the object is created, and disappears as the object disappears.
2. The local variable is present when the corresponding method is called, and when the statement that created the variable is executed, it disappears from memory as soon as its scope is out.
Initial Merit Difference:
1. The member variable has the default initial value:
Data type Default initial value
int 0
Float 0.0f
Double 0.0
Boolean false
Char ' (null character)
String NULL
2, local variables do not have a default value, you must first initialize in use.
method: A function is also called a method.

Statically-static modifier
Static modifier member variable (statically member variable)
Usage Scenario: You can use the static modifier if you have data that needs to be shared with all objects. For example the statistics of a class were used several times.
How static member variables are accessed:
1, can be accessed through the object
Format: object. Variable name
2. Access can be accessed using the class name
Format: class name. variable name
Precautions:
1. Non-static member variables can only use object access and cannot be accessed using the class name.
2. Do not use static modifier member variables for easy access to data. Static adornments are used only when the member variable data really needs to be shared.
Static modification Method (statically member method)
Usage Scenario: If a function does not have direct access to non-static members, then you can use the static modifier, which is typically used for tool type methods. ----static functions can access non-static data as long as there are objects, as long as they cannot be accessed directly.
How static member functions are accessed:
1, can be accessed through the object
Format: object. Static function name (formal parameter list);
2. Access can be accessed using the class name
Format: class name. static function name (formal parameter list);
Precautions:
1, a static function can call the class name or object, and not static function can only use the object to invoke.
2. A static function can directly access a static member, but not a non-static member directly.
Cause: A static function can be called directly using the class name, and there may not be an object at this time.
The non-static member data exists as the object exists.
3. A non-static function is a static and non-static member that can be accessed directly.
Cause: A non-static function can only be called by an object, and when the object exists, the static data already exists, not the static
The data also exists as the object is created.
4. Static function cannot appear this or Super keyword.
Cause: Because a static function can be called using the class name, once the class name is called, the object does not exist, and this
The keyword is a caller object that represents a function, which creates a conflict.

The life cycle of static data: Static member variable data takes precedence over object existence.
Using class names to access static members is recommended

The difference between a static member variable and a non-static member variable
1. Difference in function:
1. The role of static member variables share a data for all objects used.
2. The role of non-static member variables is to describe the public properties of a class of things.
2. The difference between the quantity and the storage location:
1. Static member variables are stored in the method area memory, and only one copy of the data exists.
2. Non-static member variables are stored in heap memory, and N objects have n copies of data.
3. Differences in life cycle:
1. Static member variable data is present as the class is loaded and disappears as the class file disappears.
2. Non-static member data is present as the object is created and disappears as the object is reclaimed by the garbage collector.

The code examples are as follows:
Array tool Classes
Class arraytool{
static int count = 0; Number of statistics used
{
count++;
}
public static void Showcount () {
System.out.println ("Created" +count+ "objects");
}

Convert to a string and split with ","
public static String toString (int[] arr) {
String result = "";
for (int i = 0; i < arr.length; i++) {
if (i==0) {
result+= "[" +arr[i]+ ",";
}else if (i== (arr.length-1)) {
result+= arr[i]+ "]";
}else{
Result+=arr[i]+ ",";
}
}
return result;
}

Sort from small to large
public static void sort (int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j<arr.length; j + +) {
if (Arr[i]>arr[j]) {
int temp = Arr[i];
Arr[i] = Arr[j];
ARR[J] = temp;
}
}
}
}
}

Class demo{
public static void Main (string[] args) {
Arraytool AT1 = new Arraytool ();
Arraytool AT2 = new Arraytool ();
Arraytool AT3 = new Arraytool ();
Arraytool AT4 = new Arraytool ();
Arraytool.showcount ();

Int[] arr = {12,1,456,165};
Arraytool.sort (arr);
String info = arraytool.tostring (arr);
System.out.println ("Elements of the array:" + info);
}
}

Variables, methods, and static and non-static

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.