I WAS JUST EXPERIMENTING SOMETHING.
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;
    x=1;
     int a[]=new int[11];
     public static void main(String args[])
    {
        dabba d=new dabba();
        d.x=10;
        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 :-(

Recommended Answers

All 2 Replies

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)

@JamesCherrill,
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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.