-4

Ok, so I am revisiting java after many years and I was just trying out some random program when I found out that I was having an error in the following snippet. Can someone give me any heads on how to solve this? I know that static methods will not be able to access non static variables but I created an instance for it right? Also I am not getting any heads on reading some other questions so try to help me.

 import java.io.*;
    public class phone
    {
        int x=6;
        int getx()//I also tried using this function but everything in vain
        {
            return x;
        }
    }
    public class Testing_inheritance extends phone
    {
        public static void main (String args[])throws IOException
        {   
            phone xy=new phone();
            int y=phone.x;
            y+=10;
            System.out.println("The value of x is " +y);
        }
    }
2
  • 1
    int y=phone.x; should probably be int y=xy.x;. Commented May 15, 2017 at 10:02
  • Next time please attach console outoput. Commented May 15, 2017 at 10:05

3 Answers 3

3

You probably intended to access the instance variable of the instance you created :

        phone xy = new phone();
        int y = xy.x;

Since x is not a static variable it can't be accessed without specifying an instance of the phone class.

Of course this will also fail, unless you change the access level of x to public (which is possible but not advisable - you should use getter and setter methods instead of directly manipulating instance variables from outside the class).

Sign up to request clarification or add additional context in comments.

6 Comments

Dude, both things work while using static variables and in this case it is more meaning full to use phone.x since it is a class variable
and if I do xy.x I am getting the errror : The public type phone must be defined in its own file
@IshanSrivastava But x is not a static variable, so you cannot use phone.x.
@IshanSrivastava That's an unrelated error, and it speaks for itself. Each public class must be in a separate file would name is ClassName.java.
Ok thanks, sorry I got a bit confused over there on whats static and whats not.
|
1

x is not static. You need to access it through an object reference.

Do

int y = xy.getx(); //could do xy.x, but better to access through method

Also, it's better to stick with Java naming conventions

Comments

0

Almost there, I made small but very important changes, I hope you get this otherwise just ask ;-)

Phone.java

public class Phone //<--- class with capital letter always
{
    int x=6;
    int getx()//I also tried using this function but everything in vain
    {
        return x;
    }
}

Testing_inheritance.java

import java.io.*;

    public class Testing_inheritance extends Phone
    {
        public static void main (String args[])throws IOException
        {   
            Phone xy=new Phone();
            int y= xy.getx(); //<--- principle of encapsulation
            y+=10;
            System.out.println("The value of x is " +y);
        }
    }

OR private inner class :

  import java.io.IOException;

public class Phone {
    int x = 6;

    int getx()// I also tried using this function but everything in vain
    {
        return x;
    }

    private static class Testing_inheritance extends Phone {
        public static void main(String args[]) throws IOException {
            Phone xy = new Phone();
            int y = xy.getx();
            y += 10;
            System.out.println("The value of x is " + y);
        }

    }
}

1 Comment

exactly, otherwise it should be a private class, as you can notice I separate this in two classes, Phone.java and Testing_inheritance.java, you should do the same or declare as private class

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.