What the Tech logo

What the Tech? It's as easy as 1,2,3! ( Log In | Register )

Easy as 1,2,3!
 
Reply to this topicStart new topic
> exception identifier required in java
saffionline
post 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



Hi..
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
Go to the top of the page
 
+Quote Post
Tom Herry
post 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
Go to the top of the page
 
+Quote Post
jpshortstuff
post Apr 23 2008, 12:04 PM
Post #3


SuperHelper
Group Icon

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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   9 redofromstart 220 17th November 2009 - 07:04 PM
Last post by: LDTate
No New Posts   2 JJJOOODDDYYY 94 8th November 2009 - 03:08 PM
Last post by: Noviciate
No New Posts 12 AplusWebMaster 1,484 3rd November 2009 - 03:52 PM
Last post by: AplusWebMaster
No New Posts   2 kevnet 105 21st October 2009 - 05:23 AM
Last post by: CatByte

RSS Time is now: 21st November 2009 - 05:44 PM
Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk.
Member site: Alliance of Security Analysis Professionals | UNITE Against Malware
Memory Forums | Auto Repair Forum
© Geeks to Go, Inc. | All Rights Reserved | Privacy Policy