Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Can I create private variables in a Java method?

 
Ranch Hand
Posts: 191
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IntelliJ
Java  Selenium TestNG

I can declare private variables in the constructor class but not in other methods.   Is this normal?  
It is the IDE that is complaining.

Thanks,

Kevin
 
Master Rancher
Posts: 4000
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.  Well, there's no such thing as a "constructor class".  You can declare them private immediately inside a class, but not inside a constructor (which is inside a class) or inside a method (which is also inside a class).  Inside a constructor or method, you can declare a variable, but it's a local variable, already "private" from any other context, and thus there's no point in declaring it private.
 
Marshal
Posts: 73738
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . I can declare private variables in the constructor . . .  

Please show an example of what you mean; as MS said, the keyword private will produce a compile‑time error in the constructor.
 
Ranch Hand
Posts: 92
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You declare class members in the class scope and initialize inline or in the constructor.
 
Saloon Keeper
Posts: 24174
165
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, it's not "constructor class", it's "class constructor" - meaning a special method used to construct (initialize) a class instance as it's being created.

Variables can be defined for the class (either as member or static variables), as parameters of method definitions, or withing method code blocks.

The qualifiers "public", "private" and "" (no explicit, meaning package-level) apply only to class variables. The other types exist only when their associated code is executed and live only on the thread stack for that code, being discaarded when the code or code block terminates.

You're never really "creating" variables, anyway. You just define them and then assign values to them. Sometimes you create an instance of a class, and assign a reference to that instance to a variable. That's about as close as you can get to "creating" a variable. Some other languages do allow you to "create" a new variable in their "classes", like Python and JavaScript, but that's not how Java works.
 
kevin Abel
Ranch Hand
Posts: 191
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike Simmons...I appreciate the explanation.  I should have written constructor or constructor method.  
 
kevin Abel
Ranch Hand
Posts: 191
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie... Let me attempt to code what I meant







}
 
kevin Abel
Ranch Hand
Posts: 191
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damon...Appreciated
 
kevin Abel
Ranch Hand
Posts: 191
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
Appreciated
Kevin
 
Mike Simmons
Master Rancher
Posts: 4000
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:


No - none of those "private" are allowed, including the private "pencil" variable.  Only the "pen" is okay because it's not private.  All those are local variables, inside either constructors or methods, and "private" is not allowed for local variables like that.
 
Campbell Ritchie
Marshal
Posts: 73738
332
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . Only the "pen" is okay because it's not private.  All those are local variables . . . .

But as a local variable, it only exists inside that constructor and is never used anywhere. It is quite possible that an optimising runtime will simply ignore it.
Please always start ClassNamesWithCapitalLetters. There is another compiler error in howAboutHere();  it is a method with no return type.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic