Reputation: 379
I tried calling a non-static method (sA2s)
in a static method(addGenCache)
and my IDE (JetBrains IntelliJ)
gave the
Error:(56, 27) java: non-static method sA2s(java.lang.String[]) cannot be referenced from a static context
Upon research, i discovered i had options:
1) Calling an instance of the home class(Generator
):
But the would be unhelpful giving that all the constructors for Generator
are quite elaborate and i will need to use sA2s()
in static context a number of times.
2) Creating a subclass:
I tried that here
public static void addGenCache(String genDetails[]){
record gen = new record("generators", true);
try {
sclass ss = new sclass();
gen.writeLine(ss.sA2s(genDetails));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO EXCEPTION CAUGHT");
}
}
public class sclass{
private String sA2s(String in[]){
//prepare input for export
}
}
But now i get this:
Error:(55, 21) java: non-static variable this cannot be referenced from a static context
Also if i try to initialize sclass
as static, I get an illegal start of expression
error.
Can someone please analyze code and tell me what I am doing wrong?
Also, is there an otherwise easier method to override this error?
Upvotes: 1
Views: 1393
Reputation: 12932
If you want to call a non-static method (i.e., an instance method) you need an instance of the class. It's as simple as that. So you have two possibilities: a) either make sure you have an instance of the class (this is your option 1), or b) make the method you want to call static. Which one is best in your case depends on your specific code, which you haven't provided.
Creating a subclass does not make a method static and it does not make a non-static method callable without an instance. Anyway, in your code snippet you are not creating any subclass.
You can call an instance method only if you have an instance of the class. It may be that it is cumbersome to create an instance because the constructors are very elaborate, but it may be that your instance methods needs this information in order to correctly function.
However, often you do not need to create a new instance every time you need to call the method; you can create a new instance once and use that every time:
class Foo {
Bar instance;
Foo() {
instance = new Bar(/* lots of parameters */);
}
void f() {
// ...
instance.sA2s(input1);
// ...
instance.sA2s(input2);
// ...
}
}
It can be that your method sA2a
doesn't depend on any object, for example because it only accesses static variables (and local variables) and only calls static methods. In that case you can make it static. Static methods can be called from a static context.
Upvotes: 1
Reputation: 1102
You trying to use inner class in static context as static nested. So you should chenge it so static nested:
public static class sclass
or you can use it throw referance to upper class like this:
UpperClassName upperClass = new UpperClassName();
sclass ss = upperClass.new sclass();
Upvotes: 0
Reputation: 2989
Why is sA2s private ? Try with public.
A subclass might not be the best idea as maybe the parent class have complex constructors. Can't you extract that logic into a Util class ?
The rule to call a non static public method is to hold a reference to the class (or subclass) that owns the method.
Upvotes: 0