• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this really common uses of variable names in classes  RSS feed

 
Chad McAte
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this really common uses of variable names in classes: ie numberOfWatts used for every declaration and parameters.
I have noticed they do this a lot also in java class explanation such as (Context context). Which as a beginner to programming
is really frustrating trying to understand the description when you see Context 20 times in the same sentence. Mainly want to no if
this is good programming practices. The book Im reading seams to contradict itself quite a bit like saying the naming conventions of classes should
start with an uppercase letter however all their examples you lower case for all their coding.

private int "numberOfWatts"; //wattage

public void setNumberOfWatts(int "numberOfWatts")
this."numberOfWatts" = "numberOfWatts";
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are you suggesting that "int" should be capitalized?  It's not a class, it's a primitive.

Are you saying that "void" should be capitalized?  It's not a class, it's a keyword.

"this" is also a keyword, if that's what you are asking.
 
Chad McAte
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I  brought up the book reference because they say one thing and then do another. IE class names should start with an uppercase letter yet they use all lower case letters in their class definitions like public class myclass .
No Im referring to the usage of numberOfWatts. Everything is numberOfWatts. class variable, method parameter variable. So what I am asking is whether that it's proper coding practices vs

private int "numberOfWatts"; //wattage

public void setNumberOfWatts(int "watts") // To me this seems to create some kind of separation from class code and arguments passes to its methods via objects. 
this."numberOfWatts" = "watts";              //

Sorry if this is  little confusing this is week 1 for me and all my terminology isn't greatest, just don't know how else to word it

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything is numberOfWatts. class variable, method parameter variable. So what I am asking is whether that it's proper coding practices 

Personally I think the parameter variable should be spelled differently from the class variable.  I have seen many instances of students forgetting to use the this. referenece causing the code to fail.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it is a very common style, so the students better get used to it.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that putting quotes around those names was simply your attempt to highlight the code.

Yes, using the same name in different scopes is common practice, so you best get used to it. Yes, it can be confusing to the untrained eye. The key here is to understand the implications of scope and visibility. The concepts involved are hiding and shadowing of names. A name like numberOfWatts that is used at both the method and class levels will fall under the rules of hiding and shadowing. In this case, the name at the class/instance level will be shadowed by the name at the method level. To refer to the instance variable from inside the method, you'll need to use the this keyword in front of the name. As you have seen, you can avoid having to do that by using a different name for the method parameter instead.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the JLS reference on Shadowing: https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.4.1

(Caveat: The JLS can be difficult to read/understand but it's the "official word" on the Java language)
 
Chad McAte
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TYVM
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us which book (and page number if possible) to prevent copyright problems and so we can verify the source.
Do they really call a variable numberOfWatts? Why don't they call it power? Choice of variable names can be difficult, but it makes me suspicious to see comments like // wattage
Why shou‍ld a variable name need that sort of explanation?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Really? In quotes?
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
watts wrong with you Watson?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!