Jump to content

one little question on regarding generic method in Java....

Featured Replies


public <T> void meth1(T extends Something a){..};

public <T extends Something> void meth2(T a){...};

public <T extends Something> void meth3(T extends Something a){...};

 

 

Can any one tell me the difference between these 3???

 

please help

 

thx

  • Author

btw...

 

Can any explain, with examples, the real difference between wilcard type method and generic type method in practice?

 

The only thing it seems to me is that generic type is more concrete, hence we can, for example, add elements to a collection while with wildcard type, we are like assuming to take any thing unknown....

 

please help

  • Author

ok... I understand the 1st question...please ignore...

 

for the second question, an example is:

 

static <T extends Shape> void draw(List<T> l) {drawOnCanvas(l.get(0));}

 

versus

 

static void draw(List<? extends Shape> l) {drawOnCanvas(l.get(0));}

 

 

The above code should elaborate what I mean!

 

please help

 

thx

  • Author

3rd question...

 

Simply put, why cant we declare an array of generic types???

 

please help, along with the 2nd question...

 

thx

  • Author

btw, why I cant compile this code?

public class b{

static class a{}
static class b1 extends a{}
static class c extends b{}


static <A extends b.a> void adds(LinkedList<A> l, A b1){l.[b]add(b1)[/b];}

static public void main(String[] sfg){


LinkedList<c> l = new LinkedList<c>();

adds(l, new c()); }


}

 

and the problem originates from the bold part of the code above....during compile time.

 

Please help

 

thx

btw' date=' why I cant compile this code?

public class b{

static class a{}
static class b1 extends a{}
static class c extends b{}


static <A extends b.a> void adds(LinkedList<A> l, A b1){l.[b]add(b1)[/b];}

static public void main(String[] sfg){


LinkedList<c> l = new LinkedList<c>();

adds(l, new c()); }


}

 

and the problem originates from the bold part of the code above....during compile time.

 

Please help

 

thx[/quote']

 

static class c extends b{}

 

to

 

static class c extends b1{}

 

A good example of why you should try naming classes better :P

  • Author

thx Aeternus...but can you help me with other questions on this thread? Which refer to the 2nd, 3rd, and 4th posts here.

 

thx

  • 1 month later...

Could you copy-paste the output form the compiler regarding the compile-time errors? Also, I've never seen angle brackets <> in java what’s going on here?

Could you copy-paste the output form the compiler regarding the compile-time errors? Also, I've never seen angle brackets <> in java what’s going on here?

 

The angled brackets are for passing type arguments to classes/methods etc when dealing with generics (feature new to java in 1.5).

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.