This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes class generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class generics" Watch "class generics" New topic
Author

class generics

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
public class Box<T> {}

as per generic ,the above is use to pass parameter to the class Box ,then what is the use of constructor. which one is better?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Don't really understand your question.
The <T> means that you will have to specify a class which you are using. It means whatever you pass to the constructor will be a particular type, not specified at the moment.
jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
http://java.sun.com/docs/books/tutorial/java/generics/bounded.html





At the line , �Box<Integer> integerBox = new Box<Integer>();�

It create class and passing �Interger� into it.

My question is if a class is defined as follow



Please ignore rest part of the program



Then in conductor also I can pass a value to class

What is the difference between passing through �constructor� and �Formal type parameter�
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi jacob,

When you write Box<Integer> a = new Box<Integer>();
you call it " A Box object parameterized with Integer is created"
But you have not initialized by passing value to its constructor as:

Box<Integer> box = new Box<Integer>(10);
Now you have initialized the Box objects member variable with value 10,
passed as constructor parameter.


cmbhatt
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
The formal type parameter tells the compiler what sort of data this class will contain.
The constructor parameter receives the actual value.
The fact that the word "parameter" is used in different places to mean different things can be confusing.

Is that any use?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class generics
 
Similar Threads
Question from Examsimulator
Obtaining access to Logger using a different Mechanism
Show the value in select tag
Using toString().......
forcing users to close popups