This should work. You need to declare the variable cchild to be a static member of the JavaApplicaiton1 class to be able to access it statically.
class sample { public static int x; public int y; sample() { x=0; } } public class JavaApplication1 { // NEW BIT - by making this variable static we can now access it without needing an instance of the object. static sample cchild=new sample(); public static void main(String[] args) { sample.x=0; cchild.x=9; } }
Static in Java means it is a property of the class itself and not a property of an instance object of that class's type. When using non-static properties you need to have created an object of that class's type by calling a constructor and then you can use that object's reference to call non-static methods and access non-static variables. If you don't have a copy of an object of that type then you can only call the static methods and access the static variables.
The original didn't work because although you were trying to access the static variable from a static context (inside the main method which is static) you were creating the variable you used to access the static variable (cchild) in a non-static context (in the class definition). By not labelling the cchild variable 'static' it becomes an instance variable of the JavaApplication1 class and so can only be used if you create an instance of the JavaApplication1 class by calling a constructor, and not in the main method which is created statically.
I have suggested here that you change the variable to be static so that you can access it. I think this is the easiest way for you to make progress. However, in general, if you get stuck needing to make a change like this it probably shows that you need to think more about which members need to be static and which need to be on the instance object and so just making the variable static might not always be the best thing to do.
There are a few other things you might do differently in this code example. The first is that I would suggest that you use the Java naming convention of starting the name of your classes with a capital letter (Sample instead of sample in this case) otherwise they do not look like class names to Java people.