The difference between static and non-static methods defined in the main class in Java

Source: Internet
Author: User

      Knowledge Points: 1. Application of Getter and Setter 2. Local variables and member variables (also called global variables) 3.Static keyword usage A. Shared properties of all classes after the member variable is static modified B . After the method is modified by static, the class name called in this class omits the problem, and without static, the use of the 4.This keyword must be instantiated even within this class this: is an object reference to the current class.      Simply remember, it represents an object of the current class.          Note: Who calls this method, the this within the method represents who.     This scenario: Resolve local variable hidden member variable *//Define Student class class Person {//name private String name= "Andy Lau";     Age private int age=45;          The properties shared by all objects are decorated with static and can be directly called by the class name static String country = "China";     Name gets the value public String getName () {return name;         }//Name setting value public void SetName (String name) {//name = "Brigitte"; name = name;         Rules for the use of variables: proximity principle//Here is the class name, there is no similar usage, so this is problematic//here the call can only be through the object name//This object if it exists, it should represent an object of student. So, who can represent the object of the current class?         Java provides a keyword this//student.name = name;     THIS.name = name;     }//Age Gets the value public int getage () {return ages; }//age-assigned public void Setage (int Age) {this.age = age;         }}//test class public class Personclass {public void Show ()//defines a non-static method that must be instantiated before calling {     System.out.println ("haha"); } public static void Show2 ()//However, after a static method, you can use the class name directly. function name//call, within this class, the class name can be ignored, this is the common public static ...     The origin {System.out.println ("haha your Sister");         } public static void Main (string[] args) {//Creator object person s1= new Man ();          Person s2= new Person ();         Personclass s3=new Personclass ();         S3.show ();//cannot write show directly (), because the non-static method, must be instantiated after the instance call Show2 ();         Assign a value to the member variable S1.setname ("Brigitte");         S1.setage (27); S1.country= "The United States";//The value of the static variable is shared, so the last modification is the same as the Japanese s2.country= "Japan";//The static property is best called directly with the class name, without warning Oh//static and final are Different Oh, static extern,final similar to the C language, the value of a variable defined by define//static is still modifiable, but final specifies that the value is assigned at the time of definition//Fetch data System.ou      T.println (S1.getname () + "---" +s1.getage ());//Use the class name directly. Call Name and                                                       Age is absolutely not possible. SYSTEM.OUT.PRINTLN (S2.getname () + "---" +         S2.getage ());         System.out.println (s1.country);//The static property is best not to be called with an instance of the class, warning System.out.println (s2.country);  System.out.println (person.country);//person. Follow only country,//Do not prompt for name and age Oh} }

Defines methods in the main class in Java with static and non-static differences

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.