• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using class variables  RSS feed

 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how to accomplish this, any help? I have a variable defined in a class I created, I can use it in the class I call it from, but not in one that I am trying to use it in. I am calling MyDB from the main, but I am trying to use it in AutoHistory.

My class:



My application:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you get an error message from the compiler when you try to compile your source code? If so, then what is the exact error message? Or do you get an error when you run this? If so, then what happens exactly and how does that differ from what you expected?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am having problems figuring out what exactly the problem is,

could you post only the code that is giving you trouble ?
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Do you get an error message from the compiler when you try to compile your source code? If so, then what is the exact error message? Or do you get an error when you run this? If so, then what happens exactly and how does that differ from what you expected?


AutoHistory.java:63: cannot find symbol
symbol : variable vehicleFile
location: class AutoHistory
if (vehicleFile.records != 0)
^
1 error
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:i am having problems figuring out what exactly the problem is, could you post only the code that is giving you trouble ?


 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay there we go ...

you declare vehicleFile in the main method, so it is a local variable and it's goes out of scope immediately after you leave the method ..

so, either you create the MyDB reference in the class you are using it in, or you pass it as an argument ...
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of long lines; I had to edit your post so it fits into the width of the screen.

You appear to be using == to compare Strings. Don't use == on objects unless you really need to check they are the same object. Use the equals() method instead.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:okay there we go ...

you declare vehicleFile in the main method, so it is a local variable and it's goes out of scope immediately after you leave the method ..

so, either you create the MyDB reference in the class you are using it in, or you pass it as an argument ...



Isn't there a way to declare it globally?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep,

that would be declaring it as an instance variable (or static depends on your structure) ...

like this,



be aware though, that you cannot use instance variable from a static context, which means you won't have access to it from the main method.

basically, you always should leave the main method as quickly as possible and work on the instance of the class, like this..

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!