• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Errors in variables in class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is for my programming class. I was getting errors like crazy before, but I moved my variables from main to class, now I am getting error messages in my class..........

Super confused on what I am doing wrong here

 
Marshal
Posts: 76856
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't moved your variables to the class, but to the object. They are only in the class if you mark them static.
Beware of writing your code to match a compiler error message; I think the bit in the error message about “non‑static” causes a lot of confusion. I think it make you think there is something “normal” about things being static, and I think should really say that an instance method/field/etc can only be accessed from an instance method.
I think the real problem is that you aren';t getting out of static methods soon enough. The ideal length of the main method is one statement (for some reason main method shorter than one statement have never really caught on). Winston's take on the main() method does show one statement. The first line isn't a statement but a declaration, That means that all the other code needs to go into different methods.

Watch this space: more suggestions to follow.
 
Campbell Ritchie
Marshal
Posts: 76856
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • 1: You were correct to move the variables out of the main() method.
  • 2: I think you need another class to start off the whole application, and that class should have a main() method. That means you aren't calling a main() method in your original class.
  • 3: Use a file chooser rather than hard‑coding a file name or asking for text input. Come to think of it, haven't I told you that already? Haven't I told you a lot of other things already? Didn't I write something showing how to convert something similar to your app to good OO design here? (That last post needs enhancement, and contains several errors.)
  • 4: I can scarcely envisage a less helpful name for a class than Program6. Well, maybe Program. Make sure your names for everything show what they mean.
  • I have already told you to create a Price object, maybe put Price objects into a List, and make the Price objects do the calculations. You need to move the code out of the main() method. I think you are gong to have to start again, I am afraid.
    Please avoid long lines, which are messing up the formatting of the whole thre‍ad. Your line 16 should read like this, and I shall leave you to guess what will happen when you get to line 18. (Hint: it won't be nice.)Another badly‑named variable there, I am afraid. Give that method calll an additional first argument of null.
     
    If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic