Welcome Guest to What the Tech! ( Log In | Register ) We specialize in the removal of malicious software (malware), but here you'll find free help and support for all your tech questions. We invite you to ask questions, share experiences, and learn. Explore our message boards, or register now to post messages of your own. Please Start Here. Register today (registration removes advertising)
![]() ![]() |
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
|
|
![]() SuperMember Group: Senior Class Posts: 1,516 Joined: 28-April 07 From: UK Member No.: 69,799 Operating System: Windows XP |
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 | |||
|---|---|---|---|---|---|---|---|
![]() |
0 | Tokamak | 0 | 2 minutes ago Last post by: Tokamak |
|||
![]() |
10 | drragostea | 42 | Yesterday, 12:17 PM Last post by: drragostea |
|||
![]() |
11 | saqib.sarfraz | 170 | 10th August 2008 - 05:45 PM Last post by: LDTate |
|||
![]() |
0 | AplusWebMaster | 228 | 9th July 2008 - 04:52 AM Last post by: AplusWebMaster |
|||
![]() |
2 | rpoon | 221 | 22nd June 2008 - 05:04 PM Last post by: LDTate |
|||
|
Time is now: 21st August 2008 - 02:57 PM |