Jump to content

Build Theme!
  •  
  • Infected?

WE'RE SURE THAT YOU'LL LOVE US!

Hey there! :wub: Looks like you're enjoying the discussion, but you're not signed up for an account. When you create an account, we remember exactly what you've read, so you always come right back where you left off. You also get notifications, here and via email, whenever new posts are made. You can like posts to share the love. :D Join 93083 other members! Anybody can ask, anybody can answer. Consistently helpful members may be invited to become staff. Here's how it works. Virus cleanup? Start here -> Malware Removal Forum.

Try What the Tech -- It's free!


Photo

exception identifier required in java


  • Please log in to reply
2 replies to this topic

#1 saffionline

saffionline

    New Member

  • New Member
  • Pip
  • 5 posts

Posted 17 April 2008 - 05:00 AM

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

    Advertisements

Register to Remove


#2 Tom Herry

Tom Herry

    New Member

  • New Member
  • Pip
  • 12 posts

Posted 21 April 2008 - 01:34 AM

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

Edited by Tom Herry, 21 April 2008 - 01:35 AM.


#3 jpshortstuff

jpshortstuff

    Teacher Emeritus

  • Authentic Member
  • PipPipPipPipPipPip
  • 5,710 posts

Posted 23 April 2008 - 12:04 PM

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:
class abc
{
   static int a;

   public static void main(String[] args){
		 a = 10;
		 System.out.println(a);
   }
}

class abc
{
   static int a = 10;

   public static void main(String[] args){
		System.out.println(a);
   }
}

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.

Edited by jpshortstuff, 23 April 2008 - 12:05 PM.

Proud Graduate of the TC/WTT Classroom

At weekends (GMT) I may not be able to reply promptly due to various commitments. Please be patient and I will respond as soon as I can.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Need help remembering those important computer maintenance tasks? Let SCars do it for you.

Posted Image

Related Topics



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users