This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting error while declaring a String variable in main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting error while declaring a String variable in main method" Watch "Getting error while declaring a String variable in main method" New topic
Author

Getting error while declaring a String variable in main method

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
I am getting an error when i am declaring a String variable as static. The following error is coming:

Overridden.java:21: illegal start of expression
static String output=a1.getdetails();
^


As per my understanding in static method all variables declared must be static. Please correct if i am wrong & answer this error cause also.


Regards,
Vishnu
Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 16
Hi Vishnu,

You are declaring a static variable within a static method.


Vidyakar Sharma.
Ajay Pinnaka
Greenhorn

Joined: Aug 07, 2008
Posts: 15
All variables inside a method are final variables and cannot be static,private,public or protected.
Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 16
Ajay,
It's not like that. All variables declared within a method use modifiers of that method.
Ajay Pinnaka
Greenhorn

Joined: Aug 07, 2008
Posts: 15
Sorry i'm not clear on your reply.Can you please justify with an example.Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

The only modifier a local variable can have is final. Nothing else.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vidyakar Sharma
Greenhorn

Joined: Dec 27, 2009
Posts: 16
Ajay,

Rob has already answered.
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
Rob Prime wrote:The only modifier a local variable can have is final. Nothing else.


ohk.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Yes, Rob has given the correct answer. Variables inside a method (local variables) may be marked "final". Ajay Pinnaka said they are all final, which is very confusing.
Ajay Pinnaka
Greenhorn

Joined: Aug 07, 2008
Posts: 15
I am so sorry, if i really confused you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Apology accepted.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Passed in method parameters can be marked as 'final' too.
Jim ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting error while declaring a String variable in main method
 
Similar Threads
Static
explicit cast
Static method and instance variable
Compile time constant
Using String