File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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(....)
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics