> Hey, I myself found a better method and it works.
Actually, it's a worse method, in that you sacrifice type safety and manually
do inside your code what the compiler and runtime would have done for you,
thus increasing code complexity and the likelihood of bugs, and reducing the
ability to add functionality later.
> You really do not need to define two different function.
> You can declare it as foo(Object ParameterName,....)
You can, but that's an antipattern.
> Yes, the datatype of the ParameterName is written as "Object", which
> is the superclass of any class object.
And thus completely eliminates all the benefits the language would have given
wrt type safety.
> Inside you can find out which object has been passed by using
> ParameterName.getClass().toString() which actually gives you the Name
> of the class to which this object (ParameterName) belongs to in form
> of a string.
Or you could just call getClass() on the argument inside the method, and save
the redundantly repetitive overhead of the extra parameter, but it would still
be an antipattern.
> Once you know which class' object has been passed, you now are
> literally inside that class.
The runtime always knows which class was passed. Your hack adds nothing.
> Thanks for your suggestion though. I am not sure if what I have used
> breaks down under any special case, but works fine as of now :-)
The case need not be special for it to break down.
Generics might also provide some relief for your situation.

Signature
Lew
Abhishek - 12 Dec 2007 17:21 GMT
> > Hey, I myself found a better method and it works.
>
[quoted text clipped - 37 lines]
> --
> Lew
Thanks Lew. Those suggestions seem to make a lot of sense.
Can you provide me with a good framework here please?
Thanks
Abhishek
Mark Space - 12 Dec 2007 20:02 GMT
> Thanks Lew. Those suggestions seem to make a lot of sense.
> Can you provide me with a good framework here please?
Lew did mention Generics. I think that's probably the best idea.
public class MyFoo<T> {
public void foo( T parameterName ) {
// ... do foo stuff
}
}
Then when you use it, you parameterize the type (I think I have that
terminology right). You mentioned Char, String and Integer. Examples:
MyFoo<Char> charFoo = new MyFoo<Char>();
MyFoo<String> stringFoo = new MyFoo<String>();
MyFoo<Integer> integerFoo = new MyFoo<Integer>();
Pretty simple, less work, and type safe. No disadvantages that I can
see unless you're required to use J2ME or something where these new
features may not be available.
Note: not syntax checked or compiled. And Char isn't a Java type. It's
Character. ;-) But you could declare a Char if you wanted.
Abhishek - 13 Dec 2007 12:06 GMT
> > Thanks Lew. Those suggestions seem to make a lot of sense.
> > Can you provide me with a good framework here please?
[quoted text clipped - 21 lines]
> Note: not syntax checked or compiled. And Char isn't a Java type. It's
> Character. ;-) But you could declare a Char if you wanted.
Thanks for those ideas. I am in a situation I am stuck inside with.
I have a List (LinkedList) which has is a list of objects of a
particular class.
How Do I access the member variables of any particular object in that
List?
I am not able to do
Class_Name cls = List_Objects.get(0).Var_Name;
List_Objects.get(0) actually should give me the first object in the
list right?
Thanks
Abhishek
Lew - 13 Dec 2007 15:44 GMT
> Thanks for those ideas. I am in a situation I am stuck inside with.
> I have a List (LinkedList) which has is a list of objects of a
[quoted text clipped - 4 lines]
> I am not able to do
> Class_Name cls = List_Objects.get(0).Var_Name;
Because this doesn't make sense.
> List_Objects.get(0) actually should give me the first object in the
> list right?
Yes. But Object doesn't have a member "Var_Name", so it makes no sense to
refer to it.
Let's start by suggesting that you adhere to the Java naming conventions to
make the examples clearer. Class names start with an upper-case letter,
variable and method names start with a lower-case letter. Eschew underscores.
Don't put implementation ("List") into variable names.
List foos = new LinkedList(); // or new ArrayList() or ...
You didn't show generics, though you should have, so I'm going raw for a moment.
foos.add( new Foo( "first" ));
foos.add( new Foo( "second"));
etc.
later,
Foo foo = (Foo) foos.get( 0 );
String name = foo.getName();
The problem is, someone might add() a non-Foo to the List. Then the class
cast would blow up. Generics to the rescue:
List <Foo> foos = new LinkedList <Foo> ();
foos.add( new Foo( "first" ));
foos.add( new Foo( "second"));
etc.
Now you can skipo the class cast, because the compiler won't let a non-Foo
into the list.
Foo foo = foos.get( 0 );
String name = foo.getName();
BGTW, LinkedList isn't as fast at a get() as other types of List.
> Operations that index into the list will traverse the list
> from the beginning or the end, whichever is closer to the specified index.
<http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html>

Signature
Lew
Abhishek - 13 Dec 2007 18:30 GMT
> > Thanks for those ideas. I am in a situation I am stuck inside with.
> > I have a List (LinkedList) which has is a list of objects of a
[quoted text clipped - 53 lines]
> --
> Lew
Thanks Lew. That was a great piece of advice.
I have made the necessary changes into my code.
Now what datastructure other than the list do you think is suitable if
I have thousands of Objects of type foo to feed inside?
I did read that List is quite slow and starts indexing from the
beginning or end. May I know whats the fastest?
With Regards,
Abhishek S
Lew - 14 Dec 2007 02:25 GMT
> I did read that List is quite slow and starts indexing from the
> beginning or end. May I know whats the fastest?
Huh? List isn't "quite slow". Where ever did you read such a thing?
Some implementations of List are slower at some things than others. It
doesn't make List a bad choice for what you want to do. It doesn't even make
LinkedList bad for what you want to do. It depends on what you want to do.
What do you want to do?
> I have thousands of Objects of type foo to feed inside ...
I have no clue what "feed inside" means, nor how you intend to traverse the list.
And that should be type "Foo", by convention, not "foo".

Signature
Lew
This post contains requests for TWO pieces of information.