• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Generic Method in Generic Class  RSS feed

 
J Solomon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Java 1.8 compiler output:
MyList.java:45: error: cannot find symbol
m.add(1);
^
symbol: variable m
location: class MyList<E>
where E is a type-variable:
E extends Object declared in class MyList
1 error



I am trying to define a class MyList, which i just a wrapper around an ArrayList, no real purpose, just for the sake of learning Generics. Idea here is that I create a parameterized class, MyList<E>, which holds a parameterized instance var of type List<E>. I have an add method which adds an element of type E to the List<E>. If I create an instance of MyList, call it 'm', for some reason when I try to call a method on that instance the compiler complains that 'm' cannot be found. Any help would be greatly appreciated.

Thanks!
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declared m inside a scope (the try clause) separate from where you're using it. m is only available from within the try clause. To make it work, put the add statement within the clause as well.

you should not throw or catch Exception though. When bad arguments are being passed, you should throw an instance of RuntimeException, and just let it propagate. You should not catch RuntimeExceptions since they signify a bug in your code. In this case, use IllegalArgumentException.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Solomon wrote:

Java 1.8 compiler output:


I am trying to define a class MyList, which i just a wrapper around an ArrayList, no real purpose, just for the sake of learning Generics. Idea here is that I create a parameterized class, MyList<E>, which holds a parameterized instance var of type List<E>. I have an add method which adds an element of type E to the List<E>. If I create an instance of MyList, call it 'm', for some reason when I try to call a method on that instance the compiler complains that 'm' cannot be found. Any help would be greatly appreciated.


This has nothing to do with generics. You declared a local variable in the try-block. It is not in scope outside of that try-block.

Henry
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem has nothing to do with generics, it is to do with variable scope. When you declare a variable inside a try block it is only in scope within the try block. It cannot be accessed from the catch block, the finally block if there is one, or from anywhere outside of the try block.

The same is true of any other code block such as a for loop or a while loop etc.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beaten to it twice!
 
J Solomon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the quick replies, much appreciated. Now that I see the wrong in my ways, can someone please explain how you would possibly ever use an object that is initialized with a constructor that throws an Exception. If your constructor throws an exception, then whenever you try to instantiate an instance of that object, you'll have to wrap it in a try/catch. That reference to that object will only be in scope while you are inside the try portion of the try/catch; seems pretty worthless.

BUT............. i guess as long as you allocate a var to hold whatever it is that you are trying to instantiate outside of the try and just instantiate and assign in the loop, then you are good.

Example:


Right?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's absolutely right. The exception can only be thrown when you call a method (or a constructor). Declaring the variable cannot result in an Exception, so you can do that outside the try block.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Solomon wrote: i guess as long as you allocate a var to hold whatever it is that you are trying to instantiate outside of the try and just instantiate and assign in the loop, then you are good. Right?

Except that if your constructor does throw an exception, then m will be null at line 11/55 and you will get a NullPointerException. All uses of m have to be inside the try block or you have to exit from the method in the catch block.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
J Solomon wrote: i guess as long as you allocate a var to hold whatever it is that you are trying to instantiate outside of the try and just instantiate and assign in the loop, then you are good. Right?

Except that if your constructor does throw an exception, then m will be null at line 11/55 and you will get a NullPointerException. All uses of m have to be inside the try block or you have to exit from the method in the catch block.

Or, you can forget about the try/catch entirely and just let your method throw the exception:

This is worth considering anytime you don't really have a more useful response in mind to an exception, other than printing the exception and quitting. The try-catch block is not the only way to deal with an exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!