Sign in to follow this  
unholyx

Java excel static/non static help

Recommended Posts

Sorry if this is a really nub/stupid question but I'm trying to simply read in an excel file with Java.

[b]This is my code:[/b]


import java.io.File;
import java.util.Date;
import jxl.*;

public class EXCEL{

public static void main(String args[]){
EXCEL testEX = new EXCEL();
}

Workbook test1;
Sheet sheet1;

Cell a5;

String stringa5;

public EXCEL(){

try{

[b] [/b]test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
[b]sheet1 = Workbook.getSheet(0);[/b] [b] //line that's giving me the error[/b]
}catch
(Exception e){
System.out.println("Not working");
}

a5 = sheet1.getCell(0,5);

stringa5 = a5.getContents();

}
}

[b]This is the error I get:[/b]

"non-static method getSheet(int) cannot be referenced from a static context" error.

I tried reading several mini sites where people asked the same question but redundantly they all say something along the lines of not being able to call non static from static and I have to either make everything static or make non static methods. Very confusing as I don't know what is what. So if anyone could explain with simple examples so I can understand better it'd be really helpful thanks.

Share this post


Link to post
Share on other sites
[quote name='unholyx' timestamp='1318011867' post='4870208']
Sorry if this is a really nub/stupid question but I'm trying to simply read in an excel file with Java.

[b]This is my code:[/b]


import java.io.File;
import java.util.Date;
import jxl.*;

public class EXCEL{

public static void main(String args[]){
EXCEL testEX = new EXCEL();
}

Workbook test1;
Sheet sheet1;

Cell a5;

String stringa5;

public EXCEL(){

try{

[b] [/b]test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
[b]sheet1 = Workbook.getSheet(0);[/b] [b] //line that's giving me the error[/b]
}catch
(Exception e){
System.out.println("Not working");
}

a5 = sheet1.getCell(0,5);

stringa5 = a5.getContents();

}
}

[b]This is the error I get:[/b]

"non-static method getSheet(int) cannot be referenced from a static context" error.

I tried reading several mini sites where people asked the same question but redundantly they all say something along the lines of not being able to call non static from static and I have to either make everything static or make non static methods. Very confusing as I don't know what is what. So if anyone could explain with simple examples so I can understand better it'd be really helpful thanks.
[/quote]

getSheet is not a static method, you can't call it without providing a class instance, it should be test1.getSheet(0); not Workbook.getSheet(0);

(static methods don't act on instances of a class and thus they are called by doing ClassName.method(arguments) , non static methods operate on instances and thus you call them using instanceOfClass.method(arguments)

(test1 = Workbook.getWorkbook(...) creates an instance of the Workbook class using the static getWorkbook method)

I'd also recommend against creating instances of the class containing your main function, its just .. ugly. (Personal opinion).
excel.java
[code]
public class Excel {
private Workbook test1;
private Sheet sheet1;

private Cell a5;

private String stringa5;

public Excel(){ //Java classnames tend to be in UpperCamelCase while instance, variable and method names are lowerCamelCase

try{

[b] [/b]test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
[b]sheet1 = Workbook.getSheet(0);[/b] [b] //line that's giving me the error[/b]
}catch
(Exception e){
System.out.println("Not working");
}

a5 = sheet1.getCell(0,5);

stringa5 = a5.getContents();
}
}
[/code]

ExcelTest.java (i usually name the mainclass after the program having it contain nothing apart from the main method (This might just be due to my C++ background though, i'm not sure whats idiomatic Java )
[code]
public class ExcelTest {
static void main(String args[]) {
Excel testEx = new Excel();
}
}
[/code]

Share this post


Link to post
Share on other sites
I've never programmed in Java before, nor do I have the environment to test this, but I think I see your issue. Referencing the documentation for the jexcel library [url="http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html"]http://jexcelapi.sou...l/Workbook.html[/url] the [b]getWorkbook()[/b] is a static member of the [b]Workbook[/b] class, which is fine but the [b]getSheet()[/b] is not static. Are you familiar with the static keyword?

Lets look at the offending lines of code.
[code]test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error[/code]
Line one is fine, as was said but, since the [b]getSheet()[/b] method is not a static method, it needs an instance of [b]Workbook[/b] to call it; not the [b]Workbook[/b] class itself. Line two should be as follows
[code]sheet1 = test1.getSheet(0); //line that's no longer giving me the error[/code]
Oddly enough, further down the code, you correctly use a non-static method when you write
[code]a5 = sheet1.getCell(0,5);[/code]

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1318014453' post='4870227']
getSheet is not a static method, you can't call it without providing a class instance, it should be test1.getSheet(0); not Workbook.getSheet(0);

(static methods don't act on instances of a class and thus they are called by doing ClassName.method(arguments) , non static methods operate on instances and thus you call them using instanceOfClass.method(arguments)

[/quote]

Thanks, this cleared up some things but lead me to more questions at the same time.

So after reading your post, I would assume that all the methods in the program, the .getWorkbook(), .getSheet(), .getContents(), .getCell() would all be [i]non static[/i] since they're all not ClassName.method(arg) but instead they're all methods from another class somewhere in the jxl.api.

But then you said

[quote name='SimonForsman']
(test1 = Workbook.getWorkbook(...) creates an instance of the Workbook class using the[b] static getWorkbook method[/b])
[/quote]

I don't know if maybe I'm understanding it wrong but on top you said .getSheet is a non static method but then why whould .getWorkbook be static? Aren't they both methods being called from another class rather than ClassName.method(arg);?

[quote name='SimonForsman']
[b]I'd also recommend against creating instances of the class containing your main function, its just .. ugly. (Personal opinion).[/b]
[b][/quote]
[/b]
by this you mean the "Excel testEX = new Excel();" ?
[font="Arial"] [/font]

Share this post


Link to post
Share on other sites
[quote name='AverageMidget' timestamp='1318014998' post='4870229']
I've never programmed in Java before, nor do I have the environment to test this, but I think I see your issue. Referencing the documentation for the jexcel library [url="http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html"]http://jexcelapi.sou...l/Workbook.html[/url] the [b]getWorkbook()[/b] is a static member of the [b]Workbook[/b] class, which is fine but the [b]getSheet()[/b] is not static. Are you familiar with the static keyword?

Lets look at the offending lines of code.
[code]test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error[/code]
Line one is fine, as was said but, since the [b]getSheet()[/b] method is not a static method, it needs an instance of [b]Workbook[/b] to call it; not the [b]Workbook[/b] class itself. Line two should be as follows
[code]sheet1 = test1.getSheet(0); //line that's no longer giving me the error[/code]
Oddly enough, further down the code, you correctly use a non-static method when you write
[code]a5 = sheet1.getCell(0,5);[/code]
[/quote]

Ok alright I understand it a bit better now.

Non static methods needs to be called from an instance of the class, whereas static methods can be called using the class itself.

So how do you tell the difference between a static and a non static method?

.getSheet and .getWorkbook I would assume comes from the same class and in my eyes I can see no difference as to how you can tell what is static and non static.

Thanks.

Share this post


Link to post
Share on other sites
[quote name='unholyx' timestamp='1318016219' post='4870238']
[quote name='AverageMidget' timestamp='1318014998' post='4870229']
I've never programmed in Java before, nor do I have the environment to test this, but I think I see your issue. Referencing the documentation for the jexcel library [url="http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html"]http://jexcelapi.sou...l/Workbook.html[/url] the [b]getWorkbook()[/b] is a static member of the [b]Workbook[/b] class, which is fine but the [b]getSheet()[/b] is not static. Are you familiar with the static keyword?

Lets look at the offending lines of code.
[code]test1 = Workbook.getWorkbook(new File("EXCEL.xls"));
sheet1 = Workbook.getSheet(0); //line that's giving me the error[/code]
Line one is fine, as was said but, since the [b]getSheet()[/b] method is not a static method, it needs an instance of [b]Workbook[/b] to call it; not the [b]Workbook[/b] class itself. Line two should be as follows
[code]sheet1 = test1.getSheet(0); //line that's no longer giving me the error[/code]
Oddly enough, further down the code, you correctly use a non-static method when you write
[code]a5 = sheet1.getCell(0,5);[/code]
[/quote]

Ok alright I understand it a bit better now.

Non static methods needs to be called from an instance of the class, whereas static methods can be called using the class itself.

So how do you tell the difference between a static and a non static method?

.getSheet and .getWorkbook I would assume comes from the same class and in my eyes I can see no difference as to how you can tell what is static and non static.

Thanks.
[/quote]

You can see if a method is static or not by looking at the API documentation:

Here is the documentation for the Workbook class:

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html

as you can see the createWorkbook, getWorkbook and getVersion methods are static, the rest are non static.

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1318016858' post='4870241']

You can see if a method is static or not by looking at the API documentation:

Here is the documentation for the Workbook class:

[url="http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html"]http://jexcelapi.sou...l/Workbook.html[/url]

as you can see the createWorkbook, getWorkbook and getVersion methods are static, the rest are non static.
[/quote]

Oh ok, so you can't tell just by looking at it. You always have to check the API accordingly to find out if something is static or not. Ok thanks a lot!

Share this post


Link to post
Share on other sites
[quote name='unholyx' timestamp='1318016962' post='4870242']
[quote name='SimonForsman' timestamp='1318016858' post='4870241']
You can see if a method is static or not by looking at the API documentation:

Here is the documentation for the Workbook class:

[url="http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/jxl/Workbook.html"]http://jexcelapi.sou...l/Workbook.html[/url]

as you can see the createWorkbook, getWorkbook and getVersion methods are static, the rest are non static.
[/quote]

Oh ok, so you can't tell just by looking at it. You always have to check the API accordingly to find out if something is static or not. Ok thanks a lot!
[/quote]

Your IDE might be able to pull out the relevant information for you aswell.(Try moving your mouse over the method in the code or in the autocomplete list and see if it says anything for example)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this