• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing a "main" variable from within a class method  RSS feed

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reasons explained here, please avoid "b4" and similar abbreviations.
 
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 56608
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!