• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question about Generic Method in Generic Class

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Saloon Keeper
Posts: 13814
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beaten to it twice!
 
J Solomon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4184
57
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic