Static variable and static method &java variable modifier

Source: Internet
Author: User
Tags modifier ticket
static variables and methods

Static Variables

For the member variables of the class, each object has its own member variable in memory, and the object's method action is the variable acting on the object itself, and the data of each object is independent of each other. However, in practice there are times when you need to share data between objects, and you want to use one memory area to hold a particular data, all objects share that data, or access it without any object. You can use the Java static keyword to solve this problem

With the static keyword in front of the variable, the variable becomes a static variable, and the static variable is similar to a global variable, where only one copy of it exists in memory, and all objects share it.

Class tickersall{public
static int totaltickers=100;
public int sallnum; Represents the number of votes sold at a time. Public
tickersall (int num) {
sallnum=num;
Totaltickers-=num
}
}
Class static valuetest{public
static void main (string args[]) {
System.out.println ("The total number of votes is:" + Tickersall.totaltickers);
Tickersall T1 =new Tickersall (2);
System.out.println ("sell" +t1.sallnum+ "ticket"); Accessing a member variable through
an object System.out.println ("The remaining votes are:" +t1.totaltickers); Access static variables through an object
tickersall T2 =new Tickersall (3 );
System.out.print ("sell" +t2.sallnum+ "ticket \ T");
System.out.print ("The remaining number of votes is:" +t2.totaltickers);
Tickersall t3=new Tickersall (1);
System.out.print ("sell" +t3.sallnum+ "ticket \ T");
Access static variable System.out.println by class name
("The remaining votes are:" +tickersall.totaltickers);
}

The program defines a static variable totaltickers and a member variable Sallnum,totaltickers in class Tickersall to indicate the total number of votes remaining, sallnum to store the number of votes sold each time. In the main () method, three Ticksall objects have been created, which can be seen from the results of the program running, all instance objects work together to manipulate the static variable data, and each object responds to all objects for the operation of the static variable.

You can access static properties in two ways:

Class name. Static variable

Class object. Variable

static Methods Similarly, you can define static methods by adding the static keyword before the method, which is similar to static variables and can be called directly by means of the class name. static method . A static method is not associated with any object of a class, so a static method cannot access a member variable belonging to an object. Static methods are often used to provide utilities for other classes in the application, and a large number of static methods are defined for this purpose in Java's class libraries. Many of the methods in the Java.lang.math class are defined as static methods, which can be invoked directly through the math class name.

encapsulation and abstraction object-oriented encapsulation refers to the concealment of information and implementation details. One concept that complements the encapsulation is abstraction (or interfaces called classes), where the abstract bit class defines a clear boundary, the interface (data and behavior) that the class provides to the consumer of the class.

Java access modifiers include public private and protected public: the member variables and member methods of classes decorated by public can be accessed by any other class. Private: The member variables and member methods of a class decorated by private are not directly accessible by other classes, but are directly accessible to the class that declares it, only to the class to which it is declared. Use the private modifier to hide data and implementation details. Protected: Member variables and member methods decorated by protected sadly his subclasses and the classes in the same package are accessed. A member without an access modifier is called a package (package) member and can be accessed by the class that declares it and by other classes in the same package.

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.