static keyword problem.....
-
in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam
do not. That's bad style. If you need static final values, put them in an Interface:
public interface IInterface {
public class Def{
public static final String
VALUE1= "Value1",
VALUE2 = "Value2";
}
}You can then access these values like
IInterface.Def.VALUE1
EDIT: The main-class should instance a Object. Not much more to do in the main, leave the rest to the new Object:public static void main(String[] args) {
new MyObject();
}regards Torsten I never finish anyth...
-
do not. That's bad style. If you need static final values, put them in an Interface:
public interface IInterface {
public class Def{
public static final String
VALUE1= "Value1",
VALUE2 = "Value2";
}
}You can then access these values like
IInterface.Def.VALUE1
EDIT: The main-class should instance a Object. Not much more to do in the main, leave the rest to the new Object:public static void main(String[] args) {
new MyObject();
}regards Torsten I never finish anyth...
I don't like the style you've shown here for the main method. It means that your entire application is running within the scope of MyObject's contructor, which just always feels wrong to me. A constructor is there to construct an object and get it ready to do work, not to do the actual work. I would have two classes:
public class MyApp {
public static void main(String[] args) {
MyWorker w = new MyWorker();
w.run();
}}
public class MyWorker implements Runnable {
public void run() {
// doStuff
}}
That way MyWorker can be regarded as a reusable service, and the startup code is completely separate.
-
I don't like the style you've shown here for the main method. It means that your entire application is running within the scope of MyObject's contructor, which just always feels wrong to me. A constructor is there to construct an object and get it ready to do work, not to do the actual work. I would have two classes:
public class MyApp {
public static void main(String[] args) {
MyWorker w = new MyWorker();
w.run();
}}
public class MyWorker implements Runnable {
public void run() {
// doStuff
}}
That way MyWorker can be regarded as a reusable service, and the startup code is completely separate.
-
in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam
-
in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam
A static member of a class is constrained to access only the static member of the same class. It can, however, access any non-static member (if it is accessible from outside) of any class (including the class in which it is defined) if it has an instance variable to access the non-static member.
-
in java a static method can access only other static member . But,how static main method access non-static member of other class....? Please tell me... sir/mam
Actually non-static members are accessed via an object. An static member function can access non-static member of it's class also but through an oject... like--
class TestClass{ int i_m_belongs_to_an_object_of_TestClass; // non-static data member static int i_m_belongs_to_TestClass; // static data member public static void function(){ // i_m_belongs_to_an_object_of_TestClass = 0; // wont compile i_m_belongs_to_TestClass = 0; TestClass obj = new TestClass(); obj.i_m_belongs_to_an_object_of_TestClass = 0; // will compiled but accessed through object OtherClass objOther = new OtherClass(); objOther.i_m_belongs_to_an_object_of_OtherClass = 0; // will compiled but accessed through object int someInt = OtherClass.i_m_belongs_to_OtherClass; // accessed through Class name can also be accessed by an object } } class OtherClass { int i_m_belongs_to_an_object_of_OtherClass; // non-static data member static int i_m_belongs_to_OtherClass; // static data member }code> Only one copy is allocated for static data members, one copy per object for non-static data members