今天星期四
总觉得时间过得好快
转眼间已到了四月的最后一天了
五月的天即将来临
时间过得快 真是快
弹指一挥一周又一周的时间就这样过了
真希望人生有许多奇迹发生
Thursday, April 30, 2009
Thursday, April 23, 2009
Accessing non-static member variables from static methods
Many programmers, particularly when first introduced to Java, have problems with accessing member variables from their main method. The method signature for main is marked static - meaning that we don't need to create an instance of the class to invoke the main method. For example, a Java Virtual Machine (JVM) could call the class MyApplication like this :-
MyApplication.main ( command_line_args );
This means, however, that there isn't an instance of MyApplication - it doesn't have any member variables to access! Take for example the following application, which will generate a compiler error message.
public class StaticDemo
{
public String my_member_variable = "somedata"; public static void main (String args[])
{
// Access a non-static member from static method
System.out.println ("This generates a compiler error" +
my_member_variable );
}
}
If you want to access its member variables from a non-static method (like main), you must create an instance of the object. Here's a simple example of how to correctly write code to access non-static member variables, by first creating an instance of the object.public class NonStaticDemo
{
public String my_member_variable = "somedata";
public static void main (String args[])
{
NonStaticDemo demo = new NonStaticDemo();
// Access member variable of demo
System.out.println ("This WON'T generate an error" +
demo.my_member_variable );
}
}
MyApplication.main ( command_line_args );
This means, however, that there isn't an instance of MyApplication - it doesn't have any member variables to access! Take for example the following application, which will generate a compiler error message.
public class StaticDemo
{
public String my_member_variable = "somedata"; public static void main (String args[])
{
// Access a non-static member from static method
System.out.println ("This generates a compiler error" +
my_member_variable );
}
}
If you want to access its member variables from a non-static method (like main), you must create an instance of the object. Here's a simple example of how to correctly write code to access non-static member variables, by first creating an instance of the object.public class NonStaticDemo
{
public String my_member_variable = "somedata";
public static void main (String args[])
{
NonStaticDemo demo = new NonStaticDemo();
// Access member variable of demo
System.out.println ("This WON'T generate an error" +
demo.my_member_variable );
}
}
Wednesday, April 8, 2009
Subscribe to:
Posts (Atom)
Man Utd's next generation of wonderkids
Manchester United have set off on their pre-season United States tour with a number of young stars involved, but some other talents have lef...
-
My Team have won the best dressed Halloween competition for my company Halloween Annual Dinner last friday (2009-01-09). The winner prize is...
-
就让我为大家介绍一些这里的特色: 1. 泗里奎黄梨 泗里奎小镇对面岸的一个小岛,姆基湾多年来出产一种特种黄梨,当地人称她为泗里奎黄梨 (NENAS SARIKEI)。由于小岛土壤有限,泗里奎黄梨只够供应当地的市场,而没有外销,所以很多外地人都没有机会品尝到这种多汁的甜黄梨。目前这...
-
好 热的夜晚,没有一丝的风 又多蚊虫 不敢开窗换气 让人厌烦, 又是一个 难眠 的 夜晚 当结束一整天繁忙的工作时, 在静夜 惊醒时, 枕边已潮湿一片