0

I am a beginner and I am trying to inherit the value of a variable in another class.The Value I get is 0 which is should be 10.

First class:

public class assignment
{
    int var = 10;

    public static void main(String[]args)
    {
        initialize pere = new initialize();
        System.out.println("  ");
        System.out.println(" pere.invariable: "+pere.invariable);
   }
}

Second Class inherits var from class assignment:

public class initialize
{
    public static int  invariable;

    public static void main(String[]args)
    {
       assignment variable = new assignment();
       System.out.println(" variable.var : " + variable.var);
       invariable = variable.var;
       System.out.println(" invariable: " + invariable);
    }
}

Why is pere.invariable = 0 and yet I have inherited invariable(in 2nd class) which is = 10?

eckes
  • 10,103
  • 1
  • 59
  • 71
ROY
  • 3
  • 4
  • 1
    Please consider utilizing a code-block to make your sample code more readable. – Alex Hart Jul 15 '19 at 16:26
  • @Andreas I thought I spotted "extends" somewhere in that badly formatted code dump. – GhostCat Jul 15 '19 at 16:32
  • 1
    You have two `main` methods, but only one of them executes the code that sets the value of `invariable`, and it's not the one you're executing. – JonK Jul 15 '19 at 16:32
  • 1
    You asking about `pere.invariable`, which is only present in the `assignment` version of method `main`. Since the 2 `main` methods are entirely independent of each other, none of the code in the `initialize` version of method `main` is executed, so why would you expect the value to be anything other than the default of `0`? – Andreas Jul 15 '19 at 16:33
  • And Andreas is fully correct: there is zero inheritance going on here. Assigning one int to another doesn't "connect" the two fields. You simply assign values,!? – GhostCat Jul 15 '19 at 16:34
  • Okay my apologies , I am trying to get the value of a variable in another class i.e the value of "invariable" whis 10 but pere.invariable gives me 0 – ROY Jul 15 '19 at 16:35
  • @Andreas So how do I go about it?, i would like pere.invarible(in assignment class) to obtain the value of invariable(in initialize class) – ROY Jul 15 '19 at 16:42
  • @ROY You need to run the other `main` method. – Andreas Jul 15 '19 at 16:46
  • They both run the way I want them to, the only problem is the value of pere.invariable != invariable – ROY Jul 15 '19 at 16:51
  • Just an FYI, class names should be capitalized by convention – Tom O. Jul 15 '19 at 17:28

1 Answers1

1

I think the confusion here is stemming from a few places.

There is no inheritance happening here. Inheritance is something that happens to a class. For example,

public class A {}

// B extends A
public class B extends A {}

The "entry point" for a class is normally it's constructor. main is not a constructor, it's the entry point for a Java application (it's main function). A constructor is a function labeled with the classes name. For example:

public class B {
    public B() {
        // I am inside a constructor
    }
}

The way your code is situated now, initialize main function is never called. You either need to call it manually from your assignment main function, or you need to turn it into a constructor (changing the name of it and removing the static modifier, as in my example).

This looks like a school assignment. As far as I'm aware, StackOverflow has a policy against answering questions for school assignments directly, but I hope that this answer can at least help clear up some confusion and help point you in the right direction.

Alex Hart
  • 1,663
  • 12
  • 15
  • 1
    The help centre makes no references to such a policy, it might have been policy at some point in the past but it doesn't appear to be one now. Whether that's a good thing or a bad thing is a discussion for meta! – JonK Jul 15 '19 at 16:38
  • 1
    Got it. That said, from a personal standpoint, I flat out refuse to give people answers to assignment questions :) – Alex Hart Jul 15 '19 at 16:39
  • As do I - personally I think just giving them the answer is a disservice to them as it robs them of the opportunity to learn – JonK Jul 15 '19 at 16:41
  • 3
    Kindly note this is not an assignment, I am just trying to teach myself how to code – ROY Jul 15 '19 at 16:54