How to overcome the limitations of the Java Static Main () method

I am trying to do this.

In my project I have a file called "Hello.java" which is a file with a main () argument and which is called when the program is compiled. And I have another file called MyObj.java that only has a random class that I created to test Java functions. I am trying to do this:

class Hello
{
    public MyObj an_obj;
    public static void main(String[] args)
    { 
        setObj();
    }       

    public void setObj()
    {
        this.an_obj.set_size(7);
        say("size is " + this.an_obj.get_size());
    }    
}

      

In the MyObj.java class, I have this code:

public class MyObj
{
    private int size;

    public MyObj()
    {
        //do nothing
    }

    public void set_size(int new_size)
    {
        this.size=new_size;
    }

    public int get_size()
    {
        return this.size;
    }   
}

      

This, however, gives an error:

"Cannot make static reference to non-static setObj () method of type Hello".

If I add 'static' to the setObj declaration, ie

public static void setObj()

      

Then I get:

It is not possible to make a static reference to the non-static field an_obj.

My question is, how can I accomplish what I am doing by setting and returning an object field if the only way to run the program is the Main method and the main method can only call static methods? What, how can I do anything at all with this limitation of the possibility of calling static methods only ?????

0


source to share


6 answers


You can also add "static" to a member variable, or instantiate a class from a main method. Here's some sample code for both approaches:

/* Static */
class Hello {
    public static MyObj an_obj;
    public static void main(String[] args) { 
        setObj();
    }           

    public static void setObj() {
        this.an_obj.set_size(7);
        say("size is " + this.an_obj.get_size());
    }
}

/* Non-Static */
class Hello {
    public MyObj an_obj;
    public static void main(String[] args) {
        Hello hello = new Hello(); 
        hello.setObj();
    }           

    public void setObj() {
        this.an_obj.set_size(7);
        say("size is " + this.an_obj.get_size());
    }
}

      



The main difference between the two approaches is that in the first case, "an_obj" is static, i.e. there is only one variable for the entire program. If you were creating multiple Hello objects, they would all have the same obj reference. In the second, each Hello object has its own "obj" reference, each of which can point to a different MyObj instance.

Obviously, in this simple situation it doesn't make any difference anyway, but generally speaking, static members and methods should be avoided whenever possible.

+14


source


Make both a method setObj()

and a variable, an_obj

static

or do something like:



public static void main(String[] args) {
    new Hello().setObj();
}

      

+8


source


In addition to all the previous answers, Id would only like to mention this because you don't understand the concept of a language that is not necessarily a "constraint".

+5


source


You have never created an instance MyObj

.

There are two logical places to create the required object.

  • Initializing Hello.

    public MyObj an_obj= new MyObj();
    public static void main(String[] args)
    { 
        setObj();
    }       
    
          

  • Inside main ()

    public MyObj an_obj;
    public static void main(String[] args)
    { 
        an_obj= new MyObj()
        setObj();
    }       
    
          

In any case, you must actually create the object.

Alternatively, you can do static setObj

. However, this is rarely what you intend.

The right thing to do is (1) create appropriate objects and (2) rotate control over those objects. This is what you should be doing.

class Hello
{
    public MyObj an_obj= new MyObj();
    public static void main(String[] args)
    { 
        Hello aHelloObject= new Hello();
        aHelloOject.setObj();
    }       

    public void setObj()
    {
        this.an_obj.set_size(7);
        say("size is " + this.an_obj.get_size());
    }    
}

      

+2


source


You can call an instance method (i.e. method not static

) if you have an instance of the class.

0


source


From a simple point of view, there are two different things in Java:

  • objects
  • static variables / functions

Objects are created at run time when you create them. Static things (all of them) are always present and loaded as needed.

As long as you can mix and match them in your classes, you have to be careful how you use them. A static thing can call an object, but must first create a version of it. An object, once created, can call whatever static thing it likes.

We usually create classes that are objects AND have a few static bits as well, but you can have all the static things in the class if you like, or just have all the objects based on objects.

Static material is generally frowned upon because it is essentially global, which opens up more room for weird behavior. Objects without static bits are the best and are usually thread safe as long as they don't call things outside of their own internal data (keep 95% of your code simple and safe, then handle the complex 5% in some special classes).

Paul.

0


source







All Articles