GeeCON Prague 2014*
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


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: 39095
    
  23
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: 39095
    
  23
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
 
GeeCON Prague 2014
 
subject: class generics