0

I have created an interface named Leen and created a class X that implements this interface, but when i go to main class to create a variable named x like this

Leen x;
x.printname();

The compiler gives me error:

Connect.java:10: error: variable x might not have been initialized

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • Is there any line like `x = new LeenImpl();`? – Stefan Warminski Mar 21 '17 at 08:57
  • 2
    Not with that code it doesn't, as you don't have a variable called `p`. It would say that `x` might not have been initialized though, and it's right. This has nothing to do with interfaces though. You'd get the same issue if you wrote, say, `String x; x.length();` – Jon Skeet Mar 21 '17 at 08:57
  • 1
    Also note that in your code, `x` isn't a *class*, it's a variable. You say you've created "class x" to implement the interface - that's not what you've shown at all. Please provide a [mcve] to avoid the confusion. – Jon Skeet Mar 21 '17 at 08:58
  • Ok I have this classes a) main class b) interface have method to print my name c) class App implements my interface okey ? Problem when I goto main class and I try to create variable from my class name app to use like this App Myvar like String x or int z ? How I can do that ? – Saif Al-Bashiti Mar 21 '17 at 09:26
  • Note : in my class app I have implementation from interface .. that mean I do override for the method response about print my name – Saif Al-Bashiti Mar 21 '17 at 09:27

3 Answers3

0

You can't use an interface without implementation. Implementation can be a concrete class or abstract class which implements your interface. Based on your code, clearly there is no implementation for you interface. I'm also not sure where did you use the variable p. Kindly provide your whole class so that we can help you.

  • Ok I have this classes a) main class b) interface have method to print my name c) class App implements my interface okey ? Problem when I goto main class and I try to create variable from my class name app to use like this App Myvar like String x or int z ? How I can do that ? – Saif Al-Bashiti Mar 21 '17 at 09:22
  • Note : in my class app I have implementation from interface .. that mean I do override for the method response about print my name – Saif Al-Bashiti Mar 21 '17 at 09:27
0

Suppose if u want to access in the variable with out instantiating it u can make the variable static in the class. say App is ur class name and xyz is ur interface.

class App implements xyz{
    public static int x;
    public static void printme(){
        //Your code goes here...
    }

}

Now u can access that from ur mainclass like

App app= null;
app.x= 6;
app.printme();
Murali krishna
  • 823
  • 1
  • 8
  • 23
0

As mentioned earlier, it's because you declared the variable but never instantiated it, meaning it does not exsist yet, it's just a placeholder for the type defined at the declaration, what you need to do is

Leen x; // declare the variable
x = new implementationClass(); // instantiate the variable

x.PrintName(); // then you functions

Now since the interface is used, all functions on the interface is available, but the code will attempt to use the implementation form whatever class implemented the interface and was intantiated

Why is this smart ? well say you lave a List<Leen> then all elements in the list can use the functions on Leen regardless of what implementation of Leen they were created with

Mikenno
  • 294
  • 3
  • 9
  • I need to define a data type from interface like double z ... Here I don't need to create instance from double class only write class name and variable – Saif Al-Bashiti Mar 21 '17 at 09:40
  • in that case you are refering to Wrappers, simply make the implementation have a setx() and getx() then in the implementation have x stored and acces it through the interface using getx() and setx() Also interfaces and other abstract classes are impossible to acces unless implemented and initialised – Mikenno Mar 21 '17 at 09:42