AFTER COMPILING THIS I GOT THE ERROR: "<identifier>expected x=1;"
i have got this error many times, but don't know why this occurs
and also i have made the object of the class dabba in the dabba class's main method, is it wise thing to do??????

import java.lang.*;
class dabba
     int x;
     int a[]=new int[11];
     public static void main(String args[])
        dabba d=new dabba();
        System.out.println("x di value hai"+d.x);

one thing more, if i access x without the object d in println() method, then its giving an error too, i mean i know its not a static variable, but why should it give an error in a method like println if i access without object, for ex:-

      System.out.println("x di value hai"+x);    //error

please solve my problem as i have lost touch with java from the last 7 months and 7 months losing touch with a language is like starting from 0 in that language :-(

Edited by mike_2000_17: Fixed formatting

7 Years
Discussion Span
Last Post by arshi9464

1. x=0; is an assignment statement that can only be used in a method or constructor, so you can't just have it in your class like that. You can use an initialiser as in int x = 1;
2. Making the object of the class dabba in the dabba class's main method is a very common and standard thing to do. No problem.
3. x is an instance variable; it only exists within an instance of dabba, and every instance has its own value. So to print x without specifying which instance you want just doesn't make sense.

ps: No need to import java.lang - its always automatically imported
pps. Class names should begin with a capital letter (Dabba)


well thank you so much, that helped a lot, a lot of concepts have been revised due to your valuable solution, but the 1st solution is still out of my mind, i guess i need to go through some topics again....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.