The Artima Developer Community
Sponsored Link

Java Answers Forum
Java

4 replies on 1 page. Most recent reply: Feb 19, 2005 3:36 AM by Dimitris Paras

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

Java Posted: Feb 18, 2005 4:29 AM
Reply to this message Reply
Advertisement
I am a c++ developer and i am working on java lately. I faced a problem described below.

I filled a LinkedList called "customers" with objects of class "customer". Customer objects have attributes x, y, ID. Then i want to get an object of "customers" and i write:

Object cc1=Customers.getFirst();

When i am debuging, in watch window, cc1 is recognized as a customer object. How can i work with cc1 attributes now? i write:
cc1.
and it opens a dialog with a list of functions and not attributes...

How can i work with cc1 in general?


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java Posted: Feb 18, 2005 7:31 AM
Reply to this message Reply
> I am a c++ developer and i am working on java lately. I
> faced a problem described below.
>
> I filled a LinkedList called "customers" with objects of
> class "customer". Customer objects have attributes x, y,
> ID. Then i want to get an object of "customers" and i
> write:
>
> Object cc1=Customers.getFirst();
>
> When i am debuging, in watch window, cc1 is recognized as
> a customer object. How can i work with cc1 attributes now?
> i write:
> cc1.
> and it opens a dialog with a list of functions and not
> attributes...
>
> How can i work with cc1 in general?

Not exactly sure what you are asking, but I might have
an idea...

You are on the right track, Objects in Java are somewhat
similar to Objects in C++...

String attr = cc1.someStringAttribute;
int attr2 = cc1.someIntAttr;

This all depends on the locality of your attributes
(public, private or protected), hence where you
are accessing them from (what locality) is crucial.

Usually you want to access attributes via getters.
i.e. in your Object class you will have

public String getStringAttribute(){
return someStringAttribute;
}


and you access it like so:

String attr1 = cc1.getStringAttribute();

Hope this helps...

One,
Spike

Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

Re: Java Posted: Feb 18, 2005 8:18 AM
Reply to this message Reply
Thank you very much for the idea. The only problem is that class Object it is not one of mine, it is a java library's one. And i am trying now to add the method you proposed me but i dont know how. I tried to generate a new class Object2 of my own and to inherit functionalities of class Object. I wrote :

class object2 extends object

and it doesnt work?

what have i done wrong?

thank you very very much!!!!

a h

Posts: 1
Nickname: axih
Registered: Feb, 2005

Re: Java Posted: Feb 18, 2005 4:02 PM
Reply to this message Reply
> I am a c++ developer and i am working on java lately. I
> faced a problem described below.
>
> I filled a LinkedList called "customers" with objects of
> class "customer". Customer objects have attributes x, y,
> ID. Then i want to get an object of "customers" and i
> write:
>
> Object cc1=Customers.getFirst();

You probably want to do this instead:

customer cc1 = (customer)Customers.getFirst();

The situation would actually be no different with C++, so you must excuse the lengthy explanation.

Java, like C++, is a statically-typed language. In a dynamically-typed language like Objective C, you would be able to send any object any message (and if the message can't be handled by the object, a runtime exception is raised). In Java, you can attempt to send any message to any object through java.reflection, but there is a compile-time type-checking phase that prevents you from calling method "foo" on an Object o through the syntax "o.foo()" (because Object does not implement any method named "foo()").

The conclusion of the above paragraph is that the Object class does not have attributes x, y and ID; only the customer class has those attributes, so you cannot query any Object for its y value. For example, this would not work:

Object o = new HashTable();
System.out.println("o.x = " + o.x);

And this does not work because Objects do not have attributes "x". HashTables also don't have attributes "x" and the variable "o" could be a HashTable, so the Java compiler prevents the second line from compiling to prevent the mistake of querying the "x" attribute from an object that does not have an "x" attribute.

Similarly, this will not work:

Object cc1 = // some code that returns an Object
System.out.println("cc1.x = " + cc1.x);

As you can see, it does not make a difference where the object comes from; all that the Java compiler knows about cc1 is that it's an Object and Objects don't have "x" attributes. So, you need to do this:

customer cc1 = // some code that returns a customer
System.out.println("cc1.x = " + cc1.x);

And this will work. However, now you need some code that returns a customer object. This will not work:

customer cc1 = Customers.getFirst();

And this does not work because LinkedList.getFirst returns an Object, not a customer. The Java compiler cannot know what specific subtype of Object will be returned by getFirst but it knows getFirst will return an Object or a subtype of Object. customers are objects, but not all Objects are customers, so the Java compiler does not allow this code to compile (it's being conservative since this might not be a error, but it could be an error). (Note however that all customers are Objects so if you wanted to create an Object from a customer you would not need to cast.) So, you must do this:

customer cc1 = (customer)Customers.getFirst();

The cast is a gaurantee by the programmer to the Java compiler that "yes, the object returned by getFirst will be a customer in this particular instance." If the gaurantee proves wrong, the Java runtime will throw a runtime exception, but the Java compiler performs no further checking and allows the code to compile.

Dimitris Paras

Posts: 12
Nickname: dimpars
Registered: Feb, 2005

Re: Java Posted: Feb 19, 2005 3:36 AM
Reply to this message Reply
Thank you very very much!
the line
customer cc1=(customer)(Customers.getFirst());
does excellent work!
Thx a lot!!!

Flat View: This topic has 4 replies on 1 page
Topic: Corrupt JPEG data: 1 extraneous bytes before marker 0xd9 Previous Topic   Next Topic Topic: Help Please

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use