1
public class Test{
int Trails;
int Days
     public static void main (String []args){
     if(args.length!=0){
        numT = Integer.parseInt(args[0]);
        numD = Integer.parseInt(args[1]);
     }
     Trails=numT;
     Days=numD;
     }
}

I am trying to get input from the command line and then store them into globals but because they are being done in the main, it wants me to make everything static. Is there another way I should be doing this so that I can then do things with the data?

user3277779
  • 71
  • 2
  • 4
  • 12
  • No, it doesn't want you to make everything static. – Ingo Feb 08 '14 at 16:21
  • Dude u need an Object as instance variables belong to an object instead of static variables or methods which are essentially class members and you dont need an object to use them. – Kumar Abhinav Feb 08 '14 at 16:22
  • Both answers lack the right param check. You expect two parameters. So why do you only check, that at least one parameter is set? – Hannes Feb 08 '14 at 16:32

4 Answers4

3

The main(String[] args) method gets invoked by the JVM "statically" and no actual object gets created. But there isn't any reason against creating an object of the enclosing class as such:

public class Test{
    int Trails;
    int Days

    public static void main (String[] args){
        //Create object of type Test
        Test t = new Test();

        if(args.length!=0){
            t.Trails = Integer.parseInt(args[0]);
            t.Days = Integer.parseInt(args[1]);
        }
    }
}

Of course, you can also pass the parameters through a constructor as such:

public class Test{
    int Trails;
    int Days

    public Test(int numT, int numD){
        Trails = numT;
        Days = numD;
    }

    public static void main (String[] args){
        int numT;
        int numD;

        if(args.length!=0){
            numT = Integer.parseInt(args[0]);
            numD = Integer.parseInt(args[1]);

            //Create object here
            Test t = new Test(numT, numD);
        }
    }
}
initramfs
  • 8,275
  • 2
  • 36
  • 58
0
public class Test{
int Trails;
int Days
     public static void main (String []args){
     if(args.length!=0){

       Test test = new Test();
        test.Trails= Integer.parseInt(args[0]);
        test.Days= Integer.parseInt(args[1]);
     }
}

You need an object to work with instance variables

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
0

You need to pass them to an object instance that has a constructor or a method that accepts those command line args, then sets its instance variables (say, trails and days) in the constructor or method. The only way to have instance variables is to have an instance of an object. Also, note that you shouldn't capitalize variable names. The convention is to use camelCase with the first letter being lowercase. Class names start with a capital letter.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
0

You're confusing the object notion with the static method concept:

you can have many instances of objects like Test (test2, test2, etc...) each instance will have its own life: in real life the analogy is having many cars (instances) of the class (toyota corolla) for example: all cars are made as described by the model sepecification (the class)

Then you have static methods which are methods that do not use a concrete instance: for example: security ckecks can be a static method: many cars come through a unique security check (that is not a function you can launch from within your car: it's not depending on the instance)

You can use this sample to understand better

public class Test{
  int trails;
  int days;

  public String toString() {
    return "trails :"+ trails +", days : "+ days;
  }
}

public class Launcher{
  public static void main (String []args){
     if(args.length!=0){
         Test test = new Test();
         test.trails= Integer.parseInt(args[0]);
         test.days= Integer.parseInt(args[1]);

         Test test2 = new Test();
         test2.trails= 5;
         test2.days= 2;

         Command cmd = new Command();
         cmd.doSomething(test);
         cmd.doSomething(test2);
         cmd.doSomething(test);
     }
  }
}

public class Command {
  Test lastTested;
  public void doSomething(Test data) {
    lastTested = data;
    System.out.println( "working with "+ lastTested );
  }
}

In the example above, you create an instance of Test that you fill with data. Then you create an instance of Command, that will save in its state the last executed Test instance, then print the test instance data through its toString() method.

You can create a second instance of Test with other data and pass it to the same cmd (Command instance's method doSomething()). You will then see that the instance that is inside cmd changed, but getting back to your first instance test kept the values as expected

Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40