• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Can I create private variables in a Java method?

 
Ranch Foreman
Posts: 1015
11
  • 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: 5161
83
  • 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: 80616
468
  • 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: 124
  • 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: 28654
211
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 Foreman
Posts: 1015
11
  • 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 Foreman
Posts: 1015
11
  • 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 Foreman
Posts: 1015
11
  • 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 Foreman
Posts: 1015
11
  • 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: 5161
83
  • 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: 80616
468
  • 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.
 
kevin Abel
Ranch Foreman
Posts: 1015
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking back at this old thread.   Wow I got a lot of working mixed up.  I think and hope I'm better at this now.

Thanks everybody for your patience!

Kevin
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . Thanks everybody . . .

That's a pleasure
 
reply
    Bookmark Topic Watch Topic
  • New Topic