• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing from static to private

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I posted this but didn't show up.
Anyway, thanks in advance.
I was told to change my variables from static to private, I guess, in order to learn what the difference is. I know from some reading (as much as I can do during short breaks at my job) that if I change them to private, I then have to create an instance of any object I am trying to use/create - which in my Say.java program is the english equiv of a number that is on the input line.
However, I am at a loss how to convert an input variable into an instance of an object. Never did it before, don't know how to begin.
My Say.java program works with static, but generates 5 errors when I just change the string variables to be private - which is expected, but I do not know what to do next.
Here's my partially altered program - stuck. Any help would be greatly appreciated.



Moderator edit: Next time please UseCodeTags ←(click)

Edited by JD - Please don't post code in the forums for CD submissions. Let everyone have fun making their own solutions!
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have misunderstood the instruction. The modifiers static and private are completely different. One controls access and the other is a sort of scope: things static do not belong to an object.
I would suggest that the keyword static should be the exception. Things should default to being non‑static (i.e. they belong to the instance). Things static belong to the class. If you have your fields not static, they can have different values in each object, but they have no existence until an object is created. So you should move everything out of the main method. A main method should have one statement. You can see an example here. As you can see, that creates an object and calls a method which starts the application off.

Your code does neither. It neither creates an object nor calls one of its methods to start it off. It attempts to do everything in the main method. Not object‑oriented (OO) programming at all. If you are programming OO, you would create a class which incorporates a number and some way to convert it to text. So you pass 37 and it prints thirty‑seven. I think you are going to have to start from scratch creating a class which incorporates such numbers, and only later try to print the words. So give the class a better name than Say. Give it a constructor and a field and a toString method. Then try it out and see whether you can get it to work.

And, as for private, you should mark all fields except those used as global constants private.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now Junilu has added code tags, you can see a couple more things. Some of your lines are a bit long, but another thing is the

Civile, si ergo, fortibus es in ero…

You might not have forty of them, but those six braces in a row suggest you are making heavy weather of the nested ifs. You will sometimes need nested ifs, but when you see that sort of thing, start thinking there should be an easier way to do it.

Divide that conversion into several methods. If you are really ambitious, find out about the StringJoiner class.
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Sheriff - I'll chew on this a while
Jessica
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

Get on with the chewing; it will definitely take a while. Probably a long while.
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, Campbell - I'm still chewing and trying to write what I need to do in code. As I proceed - How do I make sure my stuff is all in the same package? I really don't want to assume anything.
thanks so much for your help - I had no idea how to go from the Say.java example that uses static everything to a run class and a class with private variables and such in it.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jessica Stensrud wrote: . . . How do I make sure my stuff is all in the same package? . . .

Start with the Java Tutorials section, and this smaller section. You declare the package by writing its name after the keyword package. Very first line of the class, before the imports. What you write, that is the package your class goes into.
You should make the directory structure match the package structure: each .java file is in a directory with the same name as the package name. If there is a . in the package name, then you have packages inside packages and you need a subdirectory. Note that packages have a strange naming convention (it is all in the tutorials trail). You will probably not need com.strensrud.jessica as part of the package names at this stage. You need slightly different instructions to compile the .java files; look in this old post and read the five links there. Probably better to put the .java files in the same folders as their package names than to use -d.

By the way: how did packages come up in the first place? That appears to be the first mention of them in this thread. If you only have that one class, do you need a package name for it at all?
 
Jessica Stensrud
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You had a link to some information that had a statement that read

SUMMARY:
So, putting all that together, and assuming CashRegister and its launcher are in the same package, we get:
CashRegisterLauncher.java:



so I wanted to be careful that what I was building was all in the same package - nothing you said in a post. I don't know where this link came from - here's the text where you embedded it:

I would suggest that the keyword static should be the exception. Things should default to being non‑static (i.e. they belong to the instance). Things static belong to the class. If you have your fields not static, they can have different values in each object, but they have no existence until an object is created. So you should move everything out of the main method. A main method should have one statement. You can see an example here. As you can see, that creates an object and calls a method which starts the application off.



Oh here is the source: <https://coderanch.com/how-to/java/MainIsAPain>;
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's very difficult to follow that post.
I'm guessing some of it is supposed to be quotes from other posts and some of it new comments/questions from you, but it's not easy to tell which is which.
Can you edit your post to make it clearer ?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The original poster cannot edit the post; we have restricted that facility. I have edited it. I had to change
[quote] [/quote]blahblahblah[quote] [/quote]
to
[quote]blahblahblah[/quote]
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to "Cattle Drive" forum.

Also obscuring the submission code so as to not give away answers to other students
 
please buy my thing and then I'll have more money:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic