Answers to your tech questions
Computer forums for help with removing malicious software (malware) and improving computer security

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)

      
 
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


SuperMember
Group Icon

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
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  
0 Tokamak 0 2 minutes ago
Last post by: Tokamak
No New Posts
10 drragostea 42 Yesterday, 12:17 PM
Last post by: drragostea
No New Posts  
11 saqib.sarfraz 170 10th August 2008 - 05:45 PM
Last post by: LDTate
No New Posts
0 AplusWebMaster 228 9th July 2008 - 04:52 AM
Last post by: AplusWebMaster
No New Posts  
2 rpoon 221 22nd June 2008 - 05:04 PM
Last post by: LDTate

RSS Time is now: 21st August 2008 - 02:57 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
© Geeks to Go, Inc. | All Rights Reserved | Privacy Policy