Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Is this really common uses of variable names in classes

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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";
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5494
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it is a very common style, so the students better get used to it.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TYVM
 
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Really? In quotes?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
watts wrong with you Watson?
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic