This is a read-only archive. No new posts or registrations. Privacy Page
Software

exception identifier required in java

1 min read

This thread's last reply is from . Advice, software, and links below may be out of date — treat specific steps and download links with caution.

Looking for the outcome? Ask AI

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
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
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.

Ask AI

AI can make mistakes. Check the cited posts. Archived advice can be out-of-date

Don't include personal information. Questions and selected public posts go to OpenAI. About Ask AI