• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

i could not under stand these statements
------------------------------------------------------------------------
Type parameters must not appear in any static context of a generic type, which means that type parameters cannot be used in the declaration of static fields or methods or in static nested
types or static initializers.
----------------------------------------------------------------------
if this is true ,then why this class is compiled sucessfully;

class Gen <T>
{
public static <T> display(T t)
{
System.out.println("The value is : "+t);
}
}

In the above class i am not doing any thing.
i have created on static method.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by anil kumar:
Hi

i could not under stand these statements
------------------------------------------------------------------------
Type parameters must not appear in any static context of a generic type, which means that type parameters cannot be used in the declaration of static fields or methods or in static nested
types or static initializers.
----------------------------------------------------------------------
if this is true ,then why this class is compiled sucessfully;

class Gen <T>
{
public static <T> display(T t)
{
System.out.println("The value is : "+t);
}
}

In the above class i am not doing any thing.
i have created on static method.



Let me jump to your code first:

public static <T> display(T t) {
}
Here the T is not the T the class declares. It is just matter of same type name T. For the static method you have declared the type well before using it in the method. In fact you are using same type name in both places.

Try this code example too:


[ April 30, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Chandra
-----------------------------------
void show(T t1) {
System.out.println(t1);}
----------------------------------

change this code to this

-----------------------------------
static void show(T t1) {
System.out.println(t1);}
----------------------------------


Those lines are saying about the above static void show(....)
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,
Can you explain me the below code?


public static <T> display(T t) {
}
Here the T is not the T the class declares. It is just matter of same type name T. For the static method you have declared the type well before using it in the method. You are hiding the class type with method type (keeping same name T for both).
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Anil,

When you used T in the instance method, it's is type declared by the class, that is only applicable with instance methods.

And when you used T in the static, you must declare that before using; One should not confuse with the similar name;

You make the static method display() type as:

static <E> void display(E e1) {
...
}

Didn't you see the static method is able to take any type in its argument while instance method is not. To the instance method, you can only pass String, what the reference variable of the class MyType is parameterized with.


Hi Chandra
-----------------------------------
void show(T t1) {
System.out.println(t1); }
----------------------------------

change this code to this

-----------------------------------
static void show(T t1) {
System.out.println(t1); }
----------------------------------



You will get compiler error in the second one. Because you can't use class's
declared type with the static method. You must declare the type before using , in case of static method.


And in fact your class need not to be of parameterized type, only methods can be of generic type. But remember the type must be declared before using, in this case for static or instance methods both.
[ April 30, 2007: Message edited by: Chandra Bhatt ]
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey
Chandra

I am not speaking about static method.I am speaking about static context.
like
class A<T>
{
static int k;
public static void setK(T a)
{
--
}
}


I am sepaking about the lines which i have written on my post

i know it will give compile time error.
[ April 30, 2007: Message edited by: anil kumar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nik,



Hi Chandra,
Can you explain me the below code?


public static <T> display(T t) {
}
Here the T is not the T the class declares. It is just matter of same type name T. For the static method you have declared the type well before using it in the method.



The next line(I have removed that) is little misleading as I used the term
hiding.

This term hiding comes in place when you use the same type name for class type as well as declare the type for the instance method separately.
In this case you say that, you are hiding the type of class T with the type name T declared for the instance method.

Try the code below:




You see how, you must be consistent with the second argument of the justdoit() method, you can't pass anything else except Integer. Although you are passing primitive int that is boxed to wrapper Integer.

For the first argument you can pass anything, no matter what the class type
is, because you have separately declared a type for the instance method.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may help: Angelika Langer's Generic FAQ If you read the complete FAQ entry it explains why.

Regarding this code:


As explained previously it is equivalent to:


and the method (defined in static context) therefore does not use the type parameter T used in the containing generic class.

[ April 30, 2007: Message edited by: Barry Gaunt ]
[ April 30, 2007: Message edited by: Barry Gaunt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Anil,

Hey
Chandra

I am not speaking about static method.I am speaking about static context.
like
class A<T>
{
static int k;
public static void setK(T a)
{
--
}
}



I don't get you, What do you mean by static method and static context???
Anyways does previous explanation (the latest one) help you?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic