Java Fundamentals: The main class in Java defines methods with static and non-static differences (the former can omit the class name directly in the Wish method call, which must first be instantiated and invoked with an instance)

Source: Internet
Author: User

1  Packagelsg.ap.april4th2;2 /*3 Knowledge Points: 1. Getter and Setter application4 2. Local variables and member variables (also referred to as global variables)5 usage of 3.Static keywords6 A. Shared properties for all classes after the member variable is modified by static7 B. After the method is modified by static, the class name that is called within this class omits the problem, and no static, even within this class, must be instantiated first8 usage of 4.This keywords9 this: is an object reference to the current class. Simply remember, it represents an object of the current class. Ten  One Note: Who calls this method, the this within the method represents who.  A      - This is the scene: - resolving local variable hidden member variables the */ - //Defining student Classes - class Person - { +     //name -     PrivateString name= "Andy Lau"; +     //Age A     Private intAge=45; at     //properties shared by all objects are decorated with static and can be directly called directly by the class name -     StaticString country = "China"; -      -     //Name Get value -      PublicString getName () -     { in         returnname; -     } to  +     //Name Setting Value -      Public voidsetName (String name) the{//name = "Brigitte"; *         //name = name;//rules for the use of variables: Nearest principle $         //here is the class name, and there is no such usage, so this is problematic.Panax Notoginseng         //This call can only be called by the object name -         //if this object exists, it should represent an object of student.  the         //So, who can represent the object of the current class? Java provides a keyword this +         //student.name = name; A          This. Name =name; the     } +  -     //Age Get Value $      Public intgetage () $     { -         returnAge ; -     } the  -     //age-assigned valueWuyi      Public voidSetage (intAge ) the     { -          This. Age =Age ; Wu     } - } About  $ //Test Class -  -  Public classPersonclass - { A  +      Public voidShow ()//defines a non-static method that must be instantiated before it can be called the                        -     { $System.out.println ("haha"); the     } the      Public Static voidShow2 ()//However, after a static method, you can use the class name directly. Function name the                              //called, within this class, the class name can be ignored, which is common public static ... Origin of the     { -SYSTEM.OUT.PRINTLN ("haha your Sister")); in     } the      Public Static voidMain (string[] args) the     { About         //Create a Person object thePerson s1=NewPerson (); thePerson s2=NewPerson (); thePersonclass s3=NewPersonclass (); +S3.show ();//cannot write show directly () because non-static methods must be instantiated and invoked through an instance - Show2 (); the         //Assigning a value to a member variableBayiS1.setname ("Brigitte"); theS1.setage (27); theS1.country= "The United States";//The value of the static variable is shared, so the last modification is the whole of Japan. -S2.country= "Japan";//static properties are best called directly with the class name, not warning. -         //static and final are different Oh, static extern,final similar to the C language define the         //The value of the variable defined by static is still modifiable, but final specifies that the value is assigned at the time of definition the         //Get Data theSystem.out.println (S1.getname () + "---" +s1.getage ());//use the class name directly. Call Name and the                                                             //Age is absolutely not possible. -System.out.println (S2.getname () + "---" +s2.getage ()); theSystem.out.println (S1.country);//static properties are best not to be called with an instance of the class, warning the System.out.println (s2.country); theSystem.out.println (Person.country);//Person . Only prompts country later,94                                           //does not prompt for name and age Oh the     } the  the}

Java Fundamentals: The main class in Java defines methods with static and non-static differences (the former can omit the class name directly in the Wish method call, which must first be instantiated and invoked with an instance)

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.