aspose file tools*
The moose likes Beginning Java and the fly likes Variable Seemingly Inaccessible For Secondary Derivative Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable Seemingly Inaccessible For Secondary Derivative" Watch "Variable Seemingly Inaccessible For Secondary Derivative" New topic
Author

Variable Seemingly Inaccessible For Secondary Derivative

Levi Neuxell
Ranch Hand

Joined: Apr 03, 2013
Posts: 41

My problem is pretty simple (I think): I'm trying to make use of a variable from one derivative class, through the main, and into a different derivative. [I'm using JCreator.] I'm looking for a simple, straight-forward and simple answer. If someone is willing to give an example, please use a real, working program (it's easier to learn from real programs than while trying to interpret certain parts).
The question at hand is why can't variable "wtcc" be accessed in my currentChoices method? (Also, is this considered modular programming? If not, how could I make it so?)

Var "wtcc" manufactured in main,
Var "wtcc" given value in first derivative,
Var "wtcc" value edited and exported back to main,
Var "wtcc" exported into second derivative for user to view the value. <-- this is where it goes wrong, it gives me "error: cannot find symbol".


Main: (class carwash)
First derivative: (class washType)
Second derivative: (class current)

Error msg:

More information regarding this program: this program is a project for my high school computer science class (year 1 class; Computer Science 1 - I'll be taking AP Computer Science next year [junior year]), we're supposed to make a simple car wash program in which a person choose 3 things: size of car, type of wash, and scent of soap. I can easily do this with switches and whatnot, but I wanted to go over the top (I'm actually planning on using some key events, some sorts [perhaps selection sort], and arrays as well). I'm mostly going over the top for myself, as I want to learn more about Java and the best way to do so is by making an actual program. Please do not comment on how unorganized this program is, that's how I work: I set down all the pieces, put them together, and clean up the program after I verify that it works.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Welcome to the Ranch

Why is so much of your code marked static?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7807
    
  21

Levi Neuxell wrote:If someone is willing to give an example, please use a real, working program...

That's not likely to happen, since we're NotACodeMill (←click).

Also: please DontWriteLongLines (←click).
It makes your thread extremely hard to read.
I'd break yours up myself, but there are tons of them, so I suggest you use the 'Edit' button to correct them (remember: 80 characters max).

Until you do, you're not likely to get a lot of response; especially with that amount of code.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Levi Neuxell
Ranch Hand

Joined: Apr 03, 2013
Posts: 41

So I can use static functions? I don't know, but it's the only way I could get my program to run properly was by making each variable/method static. (Perhaps tell me an alternative keyword? It might fix the problem. Or is void the issue?)

I'm not asking for you to manufacture or fix the code for me, I just want to know why it's not allowing me to use the variable in the third class. I didn't really mean a 'working program', it was a mistake to write that; what I meant was to use actual data-types and such, but keep it basic - this is simply because I want a straight-forward answer about why it's not working, so if it's due to static or something and I get a response that's about how I shouldn't use static and then I don't get any alternatives, ultimately forcing me to search elsewhere (I'm using what I know in this program, and I'd like to learn. By telling me what keyword I should be using and what the keyword actually does I'll be able to better my work by using it efficiently). Although I guess searching might help if I ever do manage to find the right thing to use, giving me a straight-forward answer would save time. I just need an answer about what's wrong, what I should be doing, and a description of what the fix (such as a new keyword that is not in my diction) does so I can better my work by using it efficiently. You don't even need to put it in there for me (which I really don't want, I want to put the puzzle piece on), just tell me what's required...
On the note regarding the length of each code block, I'll fix (shorten/edit) it - I just didn't know what parts to select because I don't know what all you (anyone willing to help) will need in order to help. Thank you for informing me about this rule.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Levi Neuxell wrote:So I can use static functions? I don't know, but it's the only way I could get my program to run properly was by making each variable/method static. (Perhaps tell me an alternative keyword?


It's not a keyword. You need to create an instance.



Also, when you're inside a non-static method, you always have the "this" reference available:

Levi Neuxell
Ranch Hand

Joined: Apr 03, 2013
Posts: 41


[1]: So this creates a foo method that inherits all of the Foo class's variables and such? In which case, the foo method would inherit variable chicken?
[2]: Is this creating a sort of pathway to bar() for static methods, variables, and such?
[3]: if what I previously said (in [2]) is correct, this should print "mm, good"... is this the case?
[4]: or will this be what prints out the statement from the main class?
Sorry if these questions seem kind of dumb, but I'm still pretty new to Java.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Levi Neuxell wrote:
(1): So this creates a foo method that inherits all of the Foo class's variables and such? In which case, the foo method would inherit variable chicken?


No, it instantiates a Foo object, and puts a reference to in into the foo variable.

(2): What exactly is this doing? Is it creating a sort of pathway to bar() for static methods, variables, and such?


This is how you call a non-static method. It's a "pathway" yes, but it's not "for static methods." It's simply that if we want to refer to a non-static member (variable or method), then we have to indicate which object's (which instance's member we're talking about.

(3): if what I previously said is correct, this should print "mm, good"... is this the case?
(4): or will this be what prints out the statement from the main class?


Try it and see. But note that: 1) Your use of bar() is the same as the one that I said in a compile-time error, and 2) Your chicken variable is a local variable, not a member, so it's not accessible through a reference or class name. It only exists during the execution of that method, and is not a property of any object.

Sorry if these questions seem kind of dumb, but I'm still pretty new to Java.


Nothing to apologize for. We all have to start at the beginning. I do strongly suggest though that you study the sections of your text or tutorial that cover variables and objects. Here are some links that might be helpful.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
Levi Neuxell
Ranch Hand

Joined: Apr 03, 2013
Posts: 41

Thank you so much for helping! I'm actually using a Java book ("Introduction to Java Programming: Seventh Edition" by Y. Daniel Liang), so I'll look at your links and read more about variables and objects =3 Again, thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Levi Neuxell wrote: . . . the only way I could get my program to run properly was by making each variable/method static. . . .
In which case, as far as I am concerned, every occurrence of the keyword static, except in public static void main(String[] args) is a mistake.
I am afraid there are a few things you need to do, and you are not going to like me for this.
  • 1: Ctrl key, A key and del key. Yes. All that code goes in the recycle bin.
  • 2: Power button. Your computer is wasting electricity while it is running, and is actually interfering with your understanding the problem.
  • 3: Pencil, paper and eraser. The latter is by far the most important of the three pieces of hardware
  • Draw diagrams of what you want your car wash objects to look like. Remember a static field belongs to the class, not the object; if you set a static field in the car wash class to represent with wax, then every car will be washed with was until you reset that field. What happens in real life, and in real programming is that lots of people turn up with cars to wash, and some want wax, some want the carpets vacuumed, and some only want a water wash. so the type of wash is different for every wash ordered. If you have instance fields in your car wash class, each wash object can have different data in. And Jeff V has shown you how to call methods on object names.
    Apart from the main method, which has to be static, do not use the keyword static anywhere at this stage.
    You start with a car wash class and a car wash test class; the latter only has a main method. When you can get it to run satisfactorily like this:…then you know the CarWash class is all right. You can then start thinking about other classes.
    Don’t try writing the whole app at once. Do it bit by bit. It will be a lot easier, and a lot quicker.
    Levi Neuxell
    Ranch Hand

    Joined: Apr 03, 2013
    Posts: 41


    [1]: This creates 2 new classes, right? Or is it 2 new methods? If it's either, it It looks more efficient. Is this creating 2 new methods, or 2 instances (I still need to learn what an instance is, so I'll do that on my free time as well)? Never mind, this is a reference that allows a non-static method to be initialized from a static method.
    [2]: What exactly do these do? So these are testing each class for problems, correct?

    On that note about what I want the wash to do, the assignment was to allow them to select from 3 things: wash type, soap scent, and car size.

    Campbell Ritchie wrote:...as far as I am concerned, every occurrence of the keyword static, except in public static void main(String[] args) is a mistake.
    I am afraid there are a few things you need to do, and you are not going to like me for this.
  • 1: Ctrl key, A key and del key. Yes. All that code goes in the recycle bin.
  • 2: Power button. Your computer is wasting electricity while it is running, and is actually interfering with your understanding the problem.
  • 3: Pencil, paper and eraser. The latter is by far the most important of the three pieces of hardware

  • I see now that blindly attaching static to everything was pretty sloppy (not to mention lazy), and I may just start all over depending on the amount of time I have to do this project (I think it's about 5 weeks, or something, my teacher really hasn't set a date yet) I'm very likely going to start over on this program. I also see what you're saying about turning off my computer and going back to pencil and paper, but instead of wasting time at my computer so I'll write what I want down at school, then come home and think of the best ways to implement it in my program. And hey, once I'm finished getting everything I've written down into my program I'll be able to take a break! Although the break will most likely be spent asking more questions and trying to find newer, better ways to things.
    Thank you for your response. =)

    Edit: On that note about drawing the diagrams and whatnot, I actually love drawing the diagrams. It seems that with programming it's easier for me to draw out the problem then write it, as I end up confusing not only myself but also whomever I'm addressing when my words get jumbled from changing my mind about what I think is going on and such...
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7807
        
      21

    Levi Neuxell wrote:Although the break will most likely be spent asking more questions and trying to find newer, better ways to things...

    And that's just fine, because it's precisely what we're here for.

    Edit: On that note about drawing the diagrams and whatnot, I actually love drawing the diagrams. It seems that with programming it's easier for me to draw out the problem then write it, as I end up confusing not only myself but also whomever I'm addressing when my words get jumbled from changing my mind about what I think is going on and such...

    Well, that's a great start, because it's a lesson that most beginners take a long time to learn.

    Just remember the mantra: Whenever you run into problems, StopCoding (←click).

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38865
        
      23
    Levi Neuxell wrote: . . .
    [1]: This creates 2 new classes, right? Or is it 2 new methods? If it's either, it It looks more efficient. Is this creating 2 new methods, or 2 instances (I still need to learn what an instance is, so I'll do that on my free time as well)? Never mind, this is a reference that allows a non-static method to be initialized from a static method.
    [2]: What exactly do these do? So these are testing each class for problems, correct?. . .
    Yes, it is testing the car wash class for problems. It is not about how many objects you are creating or anything, but simply whether the class works. Obviously you will need lots more tests than that, unless your class is really tiny. If your class fails one of the tests, work out why, before you try anything else. If you work out why it fails, you can probably correct it easily; it is usually finding the problem that is difficult.
    Levi Neuxell
    Ranch Hand

    Joined: Apr 03, 2013
    Posts: 41

    Jeff Verdegan wrote:
    Levi Neuxell wrote:
    (2): What exactly is this doing? Is it creating a sort of pathway to bar() for static methods, variables, and such?


    This is how you call a non-static method. It's a "pathway" yes, but it's not "for static methods." It's simply that if we want to refer to a non-static member (variable or method), then we have to indicate which object's (which instance's) member we're talking about.


    Right, so I've figured out how to use referencing for different methods (static to non-static, and vice verse), do I do the same for variables? And if so, how does it look?


    String w needs to be saved inside String washTypeChoice so the user can view their choice from the main menu.
    Here's what it looks like from the cmd prompt:

    [Btw, I started over again - so much less hassle now. Thanks for the suggestion, Ritchie!]
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Winston Gutkowski wrote:
    Levi Neuxell wrote:On that note about drawing the diagrams and whatnot, I actually love drawing the diagrams. It seems that with programming it's easier for me to draw out the problem then write it, as I end up confusing not only myself but also whomever I'm addressing when my words get jumbled from changing my mind about what I think is going on and such...

    Well, that's a great start, because it's a lesson that most beginners take a long time to learn.

    Just remember the mantra: Whenever you run into problems, StopCoding (←click).


    I can't second that strongly enough. Whatever combination of diagrams, notes, pseudo-code, prose, index cards, post-its, white-board, crayon, clay sculpture, or anything else that works for you and helps you understand the problem and the process to its solution without getting tripped up in the details of the programming language, it's all good. The key is to get your head around the problem and the solution in the abstract first, and then deal with translating it into Java.
    Levi Neuxell
    Ranch Hand

    Joined: Apr 03, 2013
    Posts: 41

    Jeff Verdegan wrote:I can't second that strongly enough. Whatever combination of diagrams, notes, pseudo-code, prose, index cards, post-its, white-board, crayon, clay sculpture, or anything else that works for you and helps you understand the problem and the process to its solution without getting tripped up in the details of the programming language, it's all good. The key is to get your head around the problem and the solution in the abstract first, and then deal with translating it into Java.


    The only trip-up I'm having is a lack of knowledge... I don't know how to reference a variable without making both it and the method referring to it static. All I need is the syntax.

    I need to learn polymorphism, don't I? Or rather, I should learn inheritance, so that each class will inherit the variables provided from a different class that I create. Thanks! I'm going to create a class called varClass (variable class; class of variables) and fill it up with all the variables required and make each class inherit this class's information Megh, that's not a good idea, I'll just make my main class the super-class.

    Yes, inheritance was the answer. Now everything is in working condition; thank you guys for your help! (Gotta love OOP!)
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38865
        
      23
    Levi Neuxell wrote: . . . I don't know how to reference a variable without making both it and the method referring to it static. . . . I'm going to create a class called varClass . . .
    As I said earlier, for the time being forget there is such a thing as static.

    I don’t quite understand what you mean about varClass, but I am suspicious it is a disaster waiting to happen.
    Levi Neuxell
    Ranch Hand

    Joined: Apr 03, 2013
    Posts: 41

    Aha, right, well it didn't work precisely as I planned, I guess that there's a logical error somewhere, but there aren't any syntax errors - and that's fine by me. I'll just find my error and fix it. Ugh, I'm such a fool. So I have no idea if this would've worked or not, so I'm going to try using getters and setters then. Either that, or someone could offer me a straight answer instead of philosophies - not that I mind philosophies, it's just right now I'm trying to get an answer. More edit: Yeah, getters and setters seem to be what I've been looking for all this time.

    Edit: I'll just stop hassling you guys (for now at least ;)) and start reading up on objects and classes. Sorry for the trouble.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Variable Seemingly Inaccessible For Secondary Derivative