3

So I have a program that I'm trying to run which plots a line, square, that takes in the command line arguments say l 5 s 4 c 6. With this I plot out the shapes using a 2Darray. In my program I have a driver class, a char2D class(my class that handles the 2darray), and myLine. myLine derives from char2D using inheritance.

My question is this: Why do I get a compiling error cannot be referenced from static context? Would I have to move the switch statement that parses the command line arguments to a different method? If so would I have to do anything other than just cut the statement into a new method?

Here's my compiling error.

/home/miguel/School/project3 v2/driver.java:11: error: non-static method myLine(int) cannot be referenced from a static context myLine( size1 ); ^

Here's my driver class (Ignore the commented out sections as I have to fix these since I rewrote my program):

public class driver {

public static void main(String[] args ){
String type = args[0];
int size;

switch (type.toLowerCase().charAt( 0 )) {
    case 'l': 
        int size1;
        size1 = Integer.parseInt( args[1] );
        myLine( size1 );
        break;
//  case 's' : 
//      size2 = Integer.parseInt(args[1]);
//      mySquare( size );
//      break; 
//  case 'c' : 
//      size3 = Integer.parseInt(args[1]);
//      myCube( size );
//      break;
}

}




}

}
public static void myLine( int size ){                  
        myLine( size );
    }
    // System.out.print( "*");                  // don't need to print in here

    //myLine(int size); 
//  public int mySquare( int size ) {
    //for (i = 0; i < size ; i++)
//      mySquare( size );

}

//  public void myCube( int size ){                                     // z will be x but only have the length of it 
    //z = size + .5
    //  myCube(size )
//  }

Here's my char2D class

public class char2D {

protected int width; protected int height; protected char char2D[][];


public char2D( int width, int height ){                     //line              
    this.width = width; 
    this.height = height;
    char2D = new array[height][width];

}
public void grow( int width, int height ){              //note width and height are changing in this method
     this.height += height;
     this.width += width;
     char2D = new array[this.height][this.width];

}

public void plot( int x1, int y1 ){
    char2D[y1][x1] = '*';


}

public static toString( char2D )
    return char2D.toString();
}

Here's my myLine class:

public class myLine extends char2D{
private int size1; 

public myLine(int size1){
    super( size1, 1 );
    this.size1 = size1;
    plotLine();
    }
private void plotLine(){
    for( int i = 0; i < size1 ; i++){
        plot( 0 , i );



    }

}

}
Miguel Pinedo
  • 45
  • 1
  • 6
  • 1
    Change `public void myLine( int size )` to `public static void myLine( int size )` – almightyGOSU Jun 02 '15 at 05:34
  • I was going to give you the benefit of the doubt on one or two of those compilation errors, but you've got a whole host of them which makes formatting your code *difficult*. Please post *compilable* code. At this point, I'm not entirely clear what it is you're doing with `myLine`, as it would only ever call itself. – Makoto Jun 02 '15 at 05:56

3 Answers3

2

the problem is myLine() method, you should change:

public void myLine(int size) 

to

public static void myLine(int size)  

Static methods are managed differently than regular methods, because they are in a static context.

Here is some information that will help you better understand this:

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

By the way, if you want better, quicker responses, you should remove remarked source code when possible.

Brian
  • 414
  • 4
  • 14
  • To give a quick explanation, a static method is one that belongs to the class as a whole. It's there for you to call even if you don't instantiate a `new myLine()`. Non-static methods belong to a specific object - one instance of myLine. They don't exist until you actually instantiate a `new` object. That's why you're getting the error - you're trying to reference a method that belongs to an instance of myLine when you haven't yet created one. – tytk Jun 02 '15 at 05:41
  • Ah sorry about that. Will keep that in mind next time and thanks – Miguel Pinedo Jun 02 '15 at 05:42
1

You're getting the compile error because you're trying to call driver#myLine(int) (which is not a static method) from within driver#main(String[]) (which is a static method). The simple fix is simply to change the method signature of driver#myLine(int) to:

public static void myLine( int size ){
1337joe
  • 2,327
  • 1
  • 20
  • 25
  • did this and got 3 more errors : /home/miguel/School/project3/driver.java:30: error: class, interface, or enum expected } ^ /home/miguel/School/project3/driver.java:31: error: class, interface, or enum expected public static void myLine( int size ){ ^ /home/miguel/School/project3/driver.java:33: error: class, interface, or enum expected } ^ 3 errors [Finished in 5.2s with exit code 1] – Miguel Pinedo Jun 02 '15 at 05:38
0

Your naming convention is wrong!

Your closing blocks, methods class braces are not proper!

removed commented code for more readability.

and also you need to change your myLine method to class method if you want to call it from static method.

you can change your code as below

public class Driver {

public static void main(String[] args) {
    String type = args[0];
    int size;

    switch (type.toLowerCase().charAt(0)) {
    case 'l':
        int size1;
        size1 = Integer.parseInt(args[1]);
        myLine(size1);
        break;
    }

}

public static void myLine(int size) {
    myLine(size);
}
Prashant
  • 2,556
  • 2
  • 20
  • 26