I'm getting an error here..please help | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

I'm getting an error here..please help

https://code.sololearn.com/cnVQ32jyf1cO/?ref=app

11th Jun 2017, 4:32 PM
suryapoojary
suryapoojary - avatar
12 Réponses
+ 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.
11th Jun 2017, 5:11 PM
Limitless
Limitless - avatar
+ 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.
11th Jun 2017, 4:38 PM
Limitless
Limitless - avatar
+ 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(); } }
11th Jun 2017, 4:38 PM
TakshakSH
TakshakSH - avatar
+ 2
Note: A class can't be made static. only the methods and variables in the class can be made static
11th Jun 2017, 4:44 PM
Hassie
Hassie - avatar
+ 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"); } }
11th Jun 2017, 4:38 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 1
@Limitless thanks, never knew a class within a class could be static.
11th Jun 2017, 4:51 PM
Hassie
Hassie - avatar
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.
11th Jun 2017, 4:49 PM
Limitless
Limitless - avatar
0
We're all here to learn :D
11th Jun 2017, 5:04 PM
Limitless
Limitless - avatar
0
thanks guys
11th Jun 2017, 5:07 PM
suryapoojary
suryapoojary - avatar
0
but what's the keyword static
11th Jun 2017, 5:08 PM
suryapoojary
suryapoojary - avatar
0
so if i I don't make my class a 'static',it will not be available for main to execute?
12th Jun 2017, 12:13 AM
suryapoojary
suryapoojary - avatar
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.
12th Jun 2017, 1:42 AM
NeutronStar
NeutronStar - avatar