• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables of type Interface

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have read a code which puzzles me a little. I write the code below:


What surprises me is that the definition of a variable of type interface (Movable mov) is allowed. In effect, in this way, we don't instantiate the variable, which would be completely wrong. However I think that, in this way, we define a reference variable which points to a null value (default value for reference variables); to me, this means that move points to something, or is trying to point to something, which cannot be nothing but an object (null in this case), and I am inclined to believe that it is not correct.

What I may conclude is just the following rule (which is just a rule I deduce from the context, but which is not so clear to me): it's not allowed to instantiate interface variables, but it is correct to define interface variables, which point to null.

I am sorry, what I wrote may be not so clear: actually it's not simple to write about this stuff.

Thank you really in advance.
pagano
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pagano,

It's not necessary that mov will always point to null. Let's take a simple example

This is actually encouraged that your method should take interfaces as parameter if possible. So if you want to create a method printList(ArrayList<String> list) (parameter ArrayList is a class), you should instead try to create the method as printList(List<String> list) (here the parameter is List is interface). This is called coding to interface. There is another way to create instance of Movable, through anonymous inner class:
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pagano monello wrote:In effect, in this way, we don't instantiate the variable, which would be completely wrong.

First of all, you NEVER instantiate a variable. You instantiate a class (using the new operator) and the result is assigned to a reference variable. For example

pagano monello wrote:However I think that, in this way, we define a reference variable which points to a null value (default value for reference variables)

Almost true! You would have been spot-on if the reference variable was an instance or class variable, like in this code snippetNow when you instantiate the Fun class, reference variable m will be assigned a null value, because that's the default value for instance and class reference variables.

But in your code snippetmov is not an instance (nor class) variable, it's a method parameter. And in Java optional (method) parameters are not supported, so the value of mov will be whatever you pass to the method. Thus if you invoke the walk method like thisthen mov will be assigned to a null value (and the code will throw a NullPointerException. But null is not assigned by default to method parameters!

pagano monello wrote:What I may conclude is just the following rule (which is just a rule I deduce from the context, but which is not so clear to me): it's not allowed to instantiate interface variables, but it is correct to define interface variables, which point to null.

As I mentioned before, variables are NEVER instantiated. But I think you probably mean, you can't instantiate an interface type and that's true (*). This code won't compile
Let's create a class Ball which implements the Movable interfaceNow we can create a Ball instance and assign it to a Ball reference variable or a Movable reference variable or even an Object reference variableYou can use these reference variables to pass to the walk method as a parameter. Let's see what happensIt should be pretty obvious why the 2nd line compiles. Because Ball IS-A Movable, you can pass a Ball reference variable to a method parameter of type Movable (and thus the 1st line compiles as well). The last line doesn't compile, because Object IS-NOT-A Movable and thus o can not be used to pass to a method parameter of type Movable. Note: it does not matter that o is referring to a Ball object (and we know Ball IS-A Movable), because if a method can be invoked or not, is decided at compile time and the compiler only know the type of reference variable o, it does not know neither care about the type of the actual object.

And as you correctly stated, you can assign a null value to a Movable reference variable. In fact, you can assign null to any reference variable

Hope it helps!
Kind regards,
Roel

(*) While preparing for the OCP exam you'll see that you can "instantiate" an interface type. But it just looks like you are instantiating an interface type, in fact you'll be instantiating an anonymous inner class which implements only that interface. Because inner classes are not on the OCA exam, I won't elaborate on this topic (unless you really want to), I'm just mentioning it for completeness.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:There is another way to create instance of Movable, through anonymous inner class:

I like to add two things here:
1/ (anonymous) inner classes are not on the OCA exam so don't worry if you don't understand anything of this code (that's why I decided not to add such a code example, because the syntax looks weird and complex). Inner classes is a topic on the OCP exam.
2/ You are not really creating an instance of Movable, you are creating an instance of some anonymous inner class which just happens to implement the Movable interface
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Up to now I can only say that I am admired by the accuracy of both answer; so I can say just thank you.

I need time to read them, what you wrote to me is not banal.

Thank you, really.
pagano
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:1/ (anonymous) inner classes are not on the OCA exam so don't worry if you don't understand anything of this code (that's why I decided not to add such a code example, because the syntax looks weird and complex). Inner classes is a topic on the OCP exam.

My bad, this is my first time in OCA forum, generally I roam around in OCP forum
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I must correct myself, in fact I think that I was wrong when I said:
However I think that, in this way, we define a reference variable which points to a null value (default value for reference variables)

Now I want to refer to another code snippet, as it follows:



I the first instance variable (f1) doesn't point to any object, because I have not instantiated class Fun by using the new operator (new Fun();).
On the other hand the instance variable points to a null object. Am I right, I am not?
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when you instantiate the Fun class, reference variable m will be assigned a null value, because that's the default value for instance and class reference variables.

I do not understand in which way I am instantiating class Fun. I think I am not instantiating class Fun in this snippet.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try to explain:

With this code, I've just defined my class, no object is created. The Fun class declares a reference variable of type Movable. This variable is not initialized to null (as we've not created instance of Fun class).

After this code, I've created a reference variable of type Fun and initialized it with an object of Fun. So in the heap this will be the status

So I have reference variable fun which points to an object of class Fun. The Fun object has a reference variable mov which is null. So reference variables declared inside methods are not initialized to null by default, but reference variables in a class (instance variable) are initialized to null when instance of the class is created. I hope this is clear
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,
thank you for your explanation, I am impressed by it.

Then, I think you told me that:
  • if I use a reference variable (mov) in a class, this variable is initialized to null when an object is created (now mov is an attribute of an object of class Fun). In this case the reference variable is an instance variable.
  • on the other hand, if I use a reference variable (mov) as a method parameter, it's just a reference variable, and not an instance variable, so in this case of course it isn't attribute of an object, therefore it doesn't need to be instantiated to any value.

  • The following schema is great!



    I really like it!

    Thank you
    pagano
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    pagano monello wrote:I the first instance variable (f1) doesn't point to any object, because I have not instantiated class Fun by using the new operator (new Fun();).
    On the other hand the instance variable points to a null object. Am I right, I am not?

    f1 is an instance variable and instance variables get a default value when not initialized by the user/developer. For a reference variable (like f1), that default value is null (meaning: not referring to an object). The same applies to class/static instance variables as well. Local variables do not get a default value. So in this code, local1 does not have a value!This code compiles successfully. But be careful: when you want to use a local variable (for e.g. printing) it MUST have a value! And as just explained, it won't get a default value, so you have to explicitly set a value (if you want to use a local variable). This code still compiles successfullyBut if we try to print local1, we get a compiler errorThis compiler error can be easily fixed by assigning a value to local1 as this code illustrates

    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    pagano monello wrote:
    Now when you instantiate the Fun class, reference variable m will be assigned a null value, because that's the default value for instance and class reference variables.

    I do not understand in which way I am instantiating class Fun. I think I am not instantiating class Fun in this snippet.

    Correct! In that code snippet class Fun isn't instantiated at all.

    I just wanted to explain: when you create a new Fun instance (by calling new Fun();), reference variable m will be assigned a null value.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    pagano monello wrote:
  • if I use a reference variable (mov) in a class, this variable is initialized to null when an object is created (now mov is an attribute of an object of class Fun). In this case the reference variable is an instance variable.

  • Correct! But be careful: if the reference variable mov is marked static, it is not an instance variable but a class/static variable. Both will be initialized to null (if not assigned a value by the user/developer)

    pagano monello wrote:
  • on the other hand, if I use a reference variable (mov) as a method parameter, it's just a reference variable, and not an instance variable, so in this case of course it isn't attribute of an object, therefore it doesn't need to be instantiated to any value.

  • If mov is a method parameter or a local variable, the variable is not assigned a default value! A method parameter will always get a value when you invoke that method, you must use a value for that parameter (otherwise the code will not compile). A local variable MUST be initialized explicitly if you want to use/access the local variable. If you use a local variable without assigning a value, the code will not compile (as illustrated in one of my previous posts).
     
    pagano monello
    Ranch Hand
    Posts: 38
    1
    Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel,
    thank you for your last post. So now I think I have understood that method parameters and local variables are not assigned by default, so I must be careful:
  • local variables: I must assign them explicitly if I want to use them, otherwise the code won't compile
  • method parameters: I must be sure to pass a correct values for method parameters when invoking a method, otherwise the code won't compile


  • Thank you really
    pagano

    ps:
    so I guess that an interesting difference between a reference variable (instance or class variable) and a local parameter (or a method parameter) can be the following:
  • reference variables: are initialized by default to null when an object is created
  • local parameters and method parameters: are not assigned by default to any value
  •  
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you are correct!

    Just 2 little additions to make sure there is no miscommunication and you don't learn it wrong
  • if you don't assign a value to a local variable, the code will not compile if you use the variable (in print statement, if statement, and so on). If the local variable is not used at all, the code compiles successfully!
  • (instance or class) variables are only initialized to null by default if you don't have assigned a value explicitly.


  • And when you add the keyword final to create final variables (constants), it's get even more interesting Luckily there is an excellent post (by me ) to explain all the differences (and it also has an overview about the differences between local, instance and class variables with regard to default values )

    Hope it helps!
    Kind regards,
    Roel
     
    pagano monello
    Ranch Hand
    Posts: 38
    1
    Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the additions, really, and thank you for indicating me your post about final instance variables. I'm gonna read it, after drawn breath.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic