im unable to understand why im getting error,
here is my code
public class Droid{
//instance
int batteryLevel;
String name;
public Droid(String droidName){
//constructor
name=droidName;
batteryLevel=100;
}
public static void main(String[] args){
Droid codey=new Droid("Codey");
System.out.println(codey.droidName);
}
}
and Error is
Droid.java:15: error: cannot find symbol
System.out.println(codey.droidName);
^
symbol: variable droidName
location: variable codey of type Droid
1 error
Link for Droid project
you have droidName here:
public Droid(String droidName)
as a parameter for the constructor, parameters have a local scope, so they only exist within that method. Which is why you get an error
you want to use the instance variable
2 Likes
i’m quite confused too because in the instructions of “create a droid” it says that " Create a constructor method for the Droid
class.
The method should have one parameter of String droidName
".so i followed the instructions even knowing droidName will be out of the main method’s Scope… they should fix instructions if they know that this will provide an error and a lot of confusions.
but if you look at the hint for step 7:
public class Person {
int age;
public Person(int yearsAlive) {
age = yearsAlive;
}
public static void main(String[] args) {
Person patrick = new Person(32);
// printing the variable:
System.out.println(patrick);
// access the age field:
System.out.println(patrick.age);
}
}
you clearly see the instance variable/field is used.
learning the scope rules and OOP isn’t easy.
well for me it’s fun coding by following instructions and learning them hands on and not looking for hints that would give the answer right away…all i want is to have a clear and concise instructions since this is an e-learning site for newbie like me and it’s like the first rule in java iv’e read here in codeacademy. (to follow instructions correctly.)
but that is the problem, programming is everything except being provided with instructions
then the better question is: is java a suitable first language for you? Given its so OO without a way around it. Isn’t it better then to learn another language first?
Im stuck with step 13. with the method Energytransfer() . I still cant find the solution to it, do you have any idea of how to get this done.
Please see this topic:
How to ask good questions (and get good answers)
your reply is really difficult to understand what concepts and problems you are struggling with
2 Likes