Java Fundamentals and Knowledge points summary, about whether string s is default initialized to Null......,new an object and class static domain, is not in memory is not a place

Source: Internet
Author: User

A pen question

22. The following code runs the result: ()

Import Java.io.*;import java.util.*;p ublic class foo{public    static void Main (string[] args) {        String s;        System.out.println ("s=" + s);}    }

A code gets compiled and outputs "s="

B code gets compiled, and output "S=null"

C because string s is not initialized, the code cannot be compiled by

D code gets compiled, but catches to nullpointexception exception

Answer: C

Parsing: Starting to think it will output null or something, run it before discovering that all the defined basic types or objects in Java must be initialized to output values.

The above parsing is wrong.

The reason is that the local variables in the method must be initialized manually , otherwise the compilation will error.

Exception in thread ' main ' java.lang.Error:Unresolved compilation problem: The     local variable s may not have been Initialized

Two. In other words, the global variable does not need to be initialized manually, and the JVM is automatically initialized to null or 0.

 Public classtest00001{StaticString S; String x=8;     String y; /*The string type in the code is a local variable, then the local variable must be assigned, even if it is null, otherwise compiled, * will be prompted to say that the variable is not initialized, if it is a member of the class variable, you can not assign a value, it will have a default value. Java in order to avoid some run-time null pointer errors, will emphasize that local variables must be assigned, or the compiler will immediately error, prompting the user to modify. */     Public Static voidMain (string[] args) {Test00001 T=NewTest00001 (); System.out.println (T.S);//output NULLSystem.out.println (T.x);//output NULLSYSTEM.OUT.PRINTLN (x);//error, no initialization, or not class static variable//the non-static variable property of a class cannot be called in a method?? Even if it has been initialized.System.out.println ("s=" + s);//output NULL    }}

A global variable (that is, a class variable) has an automatic initialization value as long as it can be called. No compiler error is generated

Three. It is important, however, to create a new problem

Why can't I call a non-static member (property) in a static method (function) of a class?

The program will eventually be executed in memory, and the variable can be accessed only if it occupies a place in memory. Static members of a class (variables and methods) belong to the class itself, which allocates memory when the class is loaded, can be accessed directly through the class name, and non-static members (variables and methods) are objects of the class, so memory is allocated only when the class's object is generated (an instance of the class is created) and then accessed through the object (instance) Accessing its non-static members in a static member of a class is an error because a static member of the class already exists when the non-static member of the class does not exist, and accessing something that does not exist in memory is of course an error:class ca{private :int// non-static member, allocating memory when creating an instance of class, different instances of class corresponding to different memory regions

Yes, the appeal is the real reason.

So the main method is also a static method of the static class, so he cannot directly invoke the non-static properties and methods of the class. At this point, although you do not need to initialize the class property variables, but because you can not directly invoke non-static variables, still error.

 Public class Test04 {    String s;      Public Static void Main (string[] args) {            System.out.println (s);//Error    }}
unresolved compilation problem:        Static reference to the Non-static field s

Note that this and the beginning of the local variables must initialize the error, the essence of different!

Four. Can the non-static method of the class directly invoke the nonstatic variable?

 Public class Test02 {    String s;      Public Static void Main (string[] args) {        new  Test02 ();        T.test ();  Output NULL    }publicvoid  Test () {    System.out.println (s);}}

The reason is that when new instantiates an object of a class, all the properties and methods of the class are loaded and put into memory.

However, the following error will be added:

 Public class Test02 {    String s;      Public Static void Main (string[] args) {        new  Test02 ();        T.test ();        System.out.println (s);//Error    }publicvoid  Test () {    System.out.println (s) ;}}
unresolved compilation problem:      static reference to the non-static field s

It is still reported that a static method cannot invoke a non-static variable.

Although, the object of this class is initialized in a static method.

The reason for the moment is unclear, Java, new an object and class static domain, is not in memory is not a place, have seen a video before, it seems to be placed in

Java Fundamentals and Knowledge points summary, about whether string s is default initialized to Null......,new an object and class static domain, is not in memory is not a place

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.