
![]() ![]() |
Apr 17 2008, 05:00 AM
Post
#1
|
|
|
New Member ![]() Group: New Member Posts: 5 Joined: 29-January 08 Member No.: 76,401 Operating System: windows 98, XP |
I am new to java and need a help on this part I am declaring a variable as below and it is showing error.. as identifier required.. help me out to understand the reason behind the same.. class abc { int a; a=10; // on this point it is asking for identifier reuired public static void main(String[] args) System.out.println(a); } as i have already declared int a, why cant i assisgn a=10 in the next statement Response ASAP .... appreciated.. Thanks in advance |
|
|
|
Apr 21 2008, 01:34 AM
Post
#2
|
|
|
New Member ![]() Group: New Member Posts: 12 Joined: 21-April 08 Member No.: 78,556 Operating System: Windows XP |
Hi, Your code is
class abc { int a; a=10; // on this point it is asking for identifier reuired public static void main(String[] args) System.out.println(a); } In this code you declear a variable a; Here it is Instance Label Varialbe. You cannot access a with any instance . so, You have to define a to static or use with any instsance of it's class This post has been edited by Tom Herry: Apr 21 2008, 01:35 AM |
|
|
|
Apr 23 2008, 12:04 PM
Post
#3
|
|
![]() SuperHelper Group: Classroom Teacher Posts: 5,096 Joined: 28-April 07 From: UK Member No.: 69,799 Operating System: Windows XP (Professional), Windows Vista (Home Business), Windows 7 (Ultimate), Ubuntu Linux |
As Tom says, when you declare 'a' in your program, you are declaring it as an instance variable (also known as a 'member' of that class). These are used primarily in Object-Oriented Programming, unless they are static/final.
Again, as Tom says, you could declare it as static, or you could declare it within the main method. Here are 3 ways of achieving what you want to do with this example: CODE class abc { static int a; public static void main(String[] args){ a = 10; System.out.println(a); } } CODE class abc { static int a = 10; public static void main(String[] args){ System.out.println(a); } } CODE class abc { public static void main(String[] args){ int a; a=10; System.out.println(a); } } Additionally, "a = 10" is whats known as an assignment statement. These cannot be executed where you had yours, they need to be inside a method or constructor. Hope that helps. This post has been edited by jpshortstuff: Apr 23 2008, 12:05 PM |
|
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
9 | redofromstart | 220 | 17th November 2009 - 07:04 PM Last post by: LDTate |
|||
![]() |
2 | JJJOOODDDYYY | 94 | 8th November 2009 - 03:08 PM Last post by: Noviciate |
|||
![]() |
12 | AplusWebMaster | 1,484 | 3rd November 2009 - 03:52 PM Last post by: AplusWebMaster |
|||
![]() |
2 | kevnet | 105 | 21st October 2009 - 05:23 AM Last post by: CatByte |
|||
|
Time is now: 21st November 2009 - 05:44 PM |