• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

class generics

 
Ranch Hand
Posts: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 80222
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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�
 
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 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
Marshal
Posts: 80222
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic