-1

I found a situation that asks me to find mistakes in various coding positions. Here we have two classes

Class A which functions as a super class

package TestSes;

public class A {
    private int a = 100;

    public void setA( int value) {
        a = value;
    }
    public int getA() {
        return a;
    }
}

And this is it's subclass

package TestSes;

public class TestA extends A {
    private int a = 222;

    public static void main(String[] args) {
        System.out.println("in main(): ");
        System.out.println("a = "+a );
        a = 123;
    }
}

I want to know the mistake here. Please I want a full explanation especially concerning the variable a. How can I correct this code to make it work?

Cloo
  • 123
  • 8
  • 3
    You could just throw this in an IDE, you know.. – Jeroen Vannevel Jan 06 '15 at 03:55
  • What value do you think should be printed (100, 222 or 123)? – Paul Boddington Jan 06 '15 at 03:59
  • I think private fields cannot be instantiated. So the TestA class has its own field a (not inherited from A). So I think the printed value will be 222. – Cloo Jan 06 '15 at 04:06
  • @JeroenVannevel I am not seeking to throw this an IDE. I am trying to learn how it works. I tried throwing it but I could understand nothing. Thanks for your assistance. – Cloo Jan 06 '15 at 04:09
  • You are almost right. A has a private field a. Because it is private it is not possible to access this value from TestA without calling getA(). So if main were an *instance* method of TestA, it would print 222. But a method that is not an instance method is a static method. Because main is static is has nothing to do with a particular instance of TestA, so it wouldn't make sense to read 222 from main. – Paul Boddington Jan 06 '15 at 04:11
  • So look at the error messages it tells you and google what they do. This is so often answered already. – Jeroen Vannevel Jan 06 '15 at 04:12
  • @JeroenVannevel it would be easier to answer this and let laziness. – Cloo Jan 06 '15 at 04:16

2 Answers2

4

You try to access and assign a value to a non static variable a in your static main method. That's not possible. a must be associated with an instance of class TestA, so it can't be accessed without an instace.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Would this also be a scope issue? Can you access 'a' in any method without saying 'this.a' – Josh Engelsma Jan 06 '15 at 03:54
  • 1
    @JoshEngelsma In a non static method, you can access `a` without the `this` keyword. – Eran Jan 06 '15 at 03:55
  • Mr @Eran. Thanks for your explanation .What is the reason we cant access the non static fields from a static method? And why it is necessary to use the this operator? Can you explain things deeper ? – Cloo Jan 06 '15 at 04:22
  • 1
    @pentanol Each instance of `TestA` has its own `a` variable. When you try to access `a` from a static method, the compiler doesn't know which instance of `TestA` to take `a` from (there may not be any instance of `TestA` to take `a` from, if you never created an instance of `TestA`). `this` can be used in any non static method. In such methods, this.a and a are equivalent. Using `this.a` If you have a non static method that accepts an argument named `a`, you must refer to the instance variable `a` with `this.a` in order to distinguish between the two `a`s. – Eran Jan 06 '15 at 04:36
0

You have two concerns there in your code.

  1. Your variable a is a instance variable, means, it belongs to an object on class TestA. You can't access it without an instance ot TestA. But, in your static main method, your are accessing it without an instance of TestA

  2. Your super Class TestSes has a instance variable a, but, it won't be inherited to your subclass, since, your super class variable is private.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105