• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class generics

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic