aspose file tools*
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 Spring in Action this week in the Spring 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: 17
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: 17
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: 19719
    
  20

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: 17
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: 39408
    
  28
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: 39408
    
  28
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