A statement not understand!

Category: Java SE
 
Cyanliang
2013-05-21 10:54:18

Sponsored Links
public class Chinese {

static Chinese ObjRef = new Chinese ();

public static Chinese ; getInstance ()
{
return ObjRef ;/ / phrase how to implement?
}

public static void main (String [] args) {
; Chinese obj = Chinese.getInstance ();
}
}


Sponsored Links

Z20082006
2013-05-21 10:58:38


public static void main (String [] args) {
Chinese obj = Chinese.getInstance ();
}
This place is called the static method; calling the static method means class member variables are initialized, static Chinese ObjRef = new Chinese (); will be executed; executed only after the execution method body
return ObjRef;
wgy19861203
2013-05-21 11:02:45
public class Chinese {

static Chinese ObjRef = new Chinese ();

public static Chinese getInstance ()
{
return ObjRef ;/ / phrase how to implement?
}

public static void main (String [] args) {
; Chinese obj = Chinese.getInstance ();
}
}
/ / ---------------
1 When executing java Chinese when, JVM will load the Chinese class, load the class at the same time, the class of all static data members are initialized, it will be executed when this sentence static Chinese ObjRef = new Chinese (), is the result of the implementation of the statement creates a class object and a reference to his assignment to the static variables ObjRef.
2. subsequently performs Chinese obj = Chinese.getInstance () he generates the above reference to the object that is assigned to obj
feijifeijivx
2013-05-21 11:06:16
ObjRef is not static in it? static variables are shared by the entire class, and does not create a new object and then create a ObjRef
ting610
2013-05-21 11:10:33
landlord does not figure out a problem:
that if a class declares a static member variable, regardless of the class produced a number of objects, which have only a static variable. equivalent for these objects, global variables, these objects shared this static variable so there will not lead to "create a lot of new ObjRef" ; case
l858535399
2013-05-21 11:28:41
return ObjRef; equivalent

return Chinese.ObjRef;

where you do not know
b6666228
2013-05-21 11:30:43
is to return a Chinese object (static Chinese ObjRef = new Chinese () ;) handle
shenxianren
2013-05-21 11:44:10
up
babyzyy
2013-05-21 11:57:43
still do not understand. . Landlord is not to write singleton ah. Chinese ObjRef is a pointer to an object of class oh.
qwerty4567
2013-05-21 12:12:33
problem in static on
py435745221
2013-05-21 12:29:13
bit like a singleton pattern Ha `` `
``` But this does not write as if you write useless Column! !
chairsye
2013-05-21 12:35:22
public static void main (String [] args) {
Chinese obj = ObjRef;
;}
hanxinyanliang
2013-05-21 12:51:37
inside plus a constructor
Chinese () {} after seemingly can be used
Cinel
2013-05-21 01:08:18
This example somewhat like single-case model, but missing the point, is to declare Private constructor
hongghuizj888
2013-05-21 01:17:40
Chinese obj = Chinese.getInstance ();
then execute return ObjRef;
while ObjRef is Chinese type, create a new object time, will create a new ObjRef, and the new new ObjRef will create a new ObjRef, I think it will create a lot of new ObjRef, so that each object is only one variable
wzp7022205
2013-05-21 01:26:14
Halo ah, is not static for you, of course, only one, the class initialization creates
RYH105
2013-05-21 01:45:55
Chinese obj = Chinese.getInstance ();
then execute return ObjRef;
The ObjRef is Chinese type, create a new object, will create a new ObjRef, and the new new ObjRef will create a new ObjRef, I think it will create a lot of new ObjRef, so that each ObjRef object is only one variable
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / /
what you want to do
? ?
tanhuidongmnbvcxz
2013-05-21 02:00:23
What do not understand ah! Do not say others do not understand you clearly do not understand what ah. ^ _ ^
li648532146
2013-05-21 02:04:51
execute the java class member variable initialization (non-initialized memory allocation), will appear in two cases:
1 constructor call
2 Access to static members or static methods to access
wuxiaojj69
2013-05-21 02:24:19
public class Chinese {

{
System.out.println ("before ; ObjRef ");
}
static Chinese ObjRef = new Chinese ();
{
System.out . println ("after ObjRef");
}

public static Chinese getInstance () {
System.out.println ("enter getInstance");
return ObjRef;
}

; public static void main (String [] args) {
Chinese obj = Chinese.getInstance ();
}
}
=== enter the following:
C: \> javac Chinese.java

C: \> java Chinese
before ; ObjRef
after ObjRef
enter getInstance
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.

Page 1 of 2    «»