Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-static method cannot be referenced by static context [duplicate]

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 );



    }

}

}
like image 577
Miguel Pinedo Avatar asked Sep 18 '26 08:09

Miguel Pinedo


2 Answers

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.

like image 122
Brian Avatar answered Sep 19 '26 21:09

Brian


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 ){
like image 33
1337joe Avatar answered Sep 19 '26 21:09

1337joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!