• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Problem with creating objects and references of Generic class

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have created a Generic class. Now I want to create its objects and references. Following is my code










I am getting error, can't find symbol T1 and T2. Somebody please guide me.

Zulfi.
 
Saloon Keeper
Posts: 12622
273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The BuiltInLLApp class doesn't know what T1 and T2 are. Are they classes? Are they type parameters? You haven't declared them.
 
Zulfi Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks.

I don't know how to declare them. I saw the following link:

Java Generic Example

I saw an example of Java Generic in the above link. It is not using this type of declaration. I want to create a LL. If I want to declare them how should I declare them? Why its accepting them in the "GenericsType" class? Somebody please guide me.

Zulfi.
 
Stephan van Hulst
Saloon Keeper
Posts: 12622
273
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is an LL?

It's accepting T1 and T2 for the members of GenericsType, because you've declared those type variables in the first line when you declared GenericsType. Your BuiltInLLApp has no such declarations and they also wouldn't do you any good because you would be using them in the static main() method. Generic type parameters declared at the start of a class are only for instance members.

What you probably want to do is declare your head, tail and newNode variables in such a way that they use concrete types and not type parameters. The second code snippet under the heading "2. Java Generic Class" from the link you posted shows you how to do that with the String class.
 
Marshal
Posts: 71722
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use T1 and T2 as formal type parameters (line 1, etc.). Use single letters e.g. GenericsType<T, U>. Don't call the fields id or name, because that will make you think they will have particular uses. Your users can use any type they like, so call them t and u. Try not to let the users see those field names 
Your users will declare the object reference as type GenericsType<Integer, Double> or GenericsType<List<Foo>>, Set<Bar>> or something else.

Don't use _ in your method names. Use mixedCase(). Pease be sure to indent your code correctly; you can cause yourself no end of confusion wth badly indented code.
 
Zulfi Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your reply.



I am still getting syntax error:
')' expected
;  expected
can't find symbol String.



I tried to indent, given eight spaces, but 'public static...main..' not properly indented.

Somebody please guide me.
Zulfi.
 
Greenhorn
Posts: 16
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I think you're getting ahead of yourself. Before you look at an advanced concept like generics, you should understand constructors.

When you instantiate an object, you pass it parameters. You are passing parameters named String str and Integer id.

The two issues here are that they do not exist (the reason for "can't find symbol String.") and they have spaces.

The proper way to do this would be:



Here, I am passing a string literal, "baseball", and an integer, 1. These are defined values, whereas your previous code passes undefined values to the constructor.
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't use parameter types when calling a constructor or method.

See HowToFormatCode (that's a link).

Don't use variables with underscores.  See NamingConventions (that's also a link).
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better would be:

 
Zulfi Khan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Christopher Schneider:
I am doing Java after sometime. Thanks for your help.

Knute Snortum:

Thanks for telling me formatting styles.

Zulfi.
 
reply
    Bookmark Topic Watch Topic
  • New Topic