• Post Reply Bookmark Topic Watch Topic
  • New Topic

Launcher references object variable issue  RSS feed

 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I hope I'm not asking too many questions!

So I set out to write a program that takes two things from user: Name and Age

Then prints out "Name is Age"

I went through using a "launcher" and having a proper object: http://www.coderanch.com/how-to/java/MainIsAPain

The class names are Practice and Practice Launcher because I just use a Practice file as a sandbox environment so I understand its not correctly named.
I also understand my comments aren't great but I'm just trying to make it work.

Practice.java


PracticeLauncher,java


My Practice.userName doesnt reference the variable userName. Why is this?

Also y does this line need Practice twice?
Practice Practice = new Practice(userName, 45);


Thanks so much everyone!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Should be

Class names begin with an upper-case and variables with a lower-case.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remember what I told you about abbreviations like “y”.

That constructor does not set the fields. It declares local variables.
And make sure the fields are all given private access.

Writing Practice twice is a mistake. You declare the variable with a type, then you give a name to it. Don't give the name and type the same name, because that can only lead to dangerous confusion later on. It is called obscuring.
 
Shaman Dasuta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was able to get it to work. Just now I declare the variables in the object: Practice.java AND in PracticeLauncher.java

Also a friend showed me using a "setter" and "getter" to make this work.

Honestly it works but there are some concepts I need to look at closer. If anyone has any criticisms or resources about these issues that'd be great.

Is this the way to do this code?

Practice.java



PracticeLauncher,java



Executes as:


 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables userName and userAge should not be static and should be private.

In the conductor you should have:
this.userName = userName
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although that is a correct way to write setXXX method, you should consider whether you actually want any set methods. Find out about encapsulation; maybe by directly changing the value of a field you are breaching encapsulation.All of a sudden I am 3 years old.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!