• 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

accessing a "main" variable from within a class method

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really simplified program...



doesn't compile. Duh. How can I access a variable declared in the "main" method from within a class method?

Still-learning Stuart
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know you can't do that.

However you can do this..




Now sqlcmd is what you call an instance variable.

(removed comments inherited from OPs code)
 
Ranch Hand
Posts: 99
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the variable that you have declared in main method is a local variable and accessible to main method only.
For accessing variable between methods declare it as an instance varibale, which is accessible by all class members.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but main() is a method of myneatapp as well as domycoolthing() so I would think this would work

main.sqlcmd = "hello world from sqlcmd";

but it does not.

I'm trying out things locally as a standalone program b4 transmorgifying it into a servlet.


Thanks to all who replied,


Still-learning Stuart
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart Rogers wrote:OK, but main() is a method of myneatapp as well as domycoolthing() so I would think this would work

main.sqlcmd = "hello world from sqlcmd";

but it does not.

I'm trying out things locally as a standalone program b4 transmorgifying it into a servlet.


Thanks to all who replied,


Still-learning Stuart



What you write there does not make any sense. sqlcmd is an instance variable. So you wouldn't access it with the class name anyway (you only do that for static variables). Then again, main isn't the class name but the name of the main() method, so that is even more strange.

I would really recommend you to google for the java tutorial and get the basics before you try to dive into servlets/j2ee.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your suggestions. There's probably a better way to achieve what I want so tmorrow I'll rephrase my question and let the collective wisdom of the saloon guide me.


Thanks again,

Still-learning Stuart


 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For reasons explained here, please avoid "b4" and similar abbreviations.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you "send" the value of the string inside the parenthesis of the method call?



.... or is this a misunderstanding of syntax?

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

Janeice DelVecchio wrote:Can't you "send" the value of the string inside the parenthesis of the method call?

Haven't got the time to read the whole thing just now, but I think the answer is














yes
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Janeice DelVecchio wrote:Can't you "send" the value of the string inside the parenthesis of the method call?

Haven't got the time to read the whole thing just now, but I think the answer is



But the OP wants to change the value of the string reference. Changing a string parameter reference has no effect to the reference used to pass the string to the method.

Henry
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I understood from the original post.....



If he manipulates the value in the domycoolthing method, he is keeping the original value (as sqlcmd) in the main method.... and manipulating its COPY in the domycoolthing() method. In effect, with my changes:



... I think this is what we're getting at?

--Janeice
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are correct, Janeice.

You cannot manipulate the contents of a String, since String is an immutable class. You can only replace the String with another String.
 
brevity is the soul of wit - shakepeare. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic