0
I'm getting an error here..please help
12 Respuestas
+ 1
"In computer programming, a static variable is a variable that has been allocated statically so that its lifetime or "extent" extends across the entire run of the program"
In simple terms: When a program is running, all static methods are accessable without needing to declare an instance.
If a method in a class is static, your program can access that method without setting it as a variable.
+ 3
A static method cannot access a non static method.
A static method is like a constant.
If you make the class static that would solve your problem.
+ 3
public class Program
{
public static class Fruit{
void taste(){
System.out.println("sweet");
}
}
public static void main(String[] args) {
Fruit banana = new Fruit();
banana.taste();
}
}
+ 2
Note: A class can't be made static. only the methods and variables in the class can be made static
+ 1
Take out the Fruit class
public class Program
{
public static void main(String[] args) {
Fruit banana = new Fruit();
banana.taste();
}
}
public class Fruit{
void taste(){
System.out.println("sweet");
}
}
+ 1
@Limitless thanks, never knew a class within a class could be static.
0
@Hassie if you look at his code, you'll see that he created a class inside of his class. In this case the Class Fruit needs to be static. Unless he moves the class Fruit outside of his main class.
0
We're all here to learn :D
0
thanks guys
0
but what's the keyword static
0
so if i I don't make my class a 'static',it will not be available for main to execute?
0
@Hassie,
Nested classes can be made static. It cannot access non-static fields and methods of outer class. It can access static fields and methods of outer class, including private fields and methods.