3

I have this code in java I write it by netbeans

class sample { public static int x; public int y; sample() { x=0; } } public class JavaApplication1 { /** * @param args the command line arguments */ sample cchild=new sample(); public static void main(String[] args) { // TODO code application logic here sample.x=0; cchild.x=9; // here error } 

explain the sample :

I make composition to class sample , class sample contain static variable x , But when I try to access to static variable x from the instance cchild in static method the compiler make error, so in java I can not access to object in static methode even the instance contain static member ???

5
  • ok , But I can access by cchild to static member , it allow me to access to static member by the name of class , why I can not access to static member by name of instance in static methode ?@Johnny Mopp Commented Jan 4, 2017 at 13:49
  • if the methode not static I can refer a static field through object @Abhishek Commented Jan 4, 2017 at 13:50
  • @kernal I think you are misunderstanding...the problem is not with accessing x through cchild...that's possible...the problem is accessing cchild (a non-static) inside a static function Commented Jan 4, 2017 at 13:54
  • 1
    @Abhishek Your statement : "Static field can be used through class only.". is not true. Commented Jan 4, 2017 at 14:01
  • ok now I get it , thanks @Rakesh G R Commented Jan 4, 2017 at 14:03

4 Answers 4

3

The first thing you need to understand is that static members belong to a class and not an instance and can therefore be accessed directly without the need to create a reference to an instance of the class. The following statement accesses the static member x in class sample where sample is the class name and x is the static member in sample :

sample.x=0;

The following statement on the other hand does not work because the reference cchild is not static and is thus an instance field while main is a static method. An instance field cannot be accessed in a static method without a reference to an instance of the class.

cchild.x=9

For the above statement to work, you either declare cchild as static in JavaApplication1 or create an instance of JavaApplication1 in main as shown below :

JavaApplication1 instanceOfJApp = new JavaApplication1(); instanceOfJApp.cchild.x=9; 
Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

Please add at least a short explanation to your answer. Code-only answers are only helpful to those fully understanding the code - and that audience wouldn't look into this question.
2

There are 2 things you can do to fix your problem:

  1. Make cchild static

  2. Move the declaration of cchild to your main method

Comments

1

It's because invoking

static void main(String[] args){ } 

doesn't make the JavaApplication1 instance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.