aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object reference" Watch "object reference" New topic
Author

object reference

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100


in this code !! how does an object new mineral() here refer to the reference
mineral m
in the getweight(mineral m) method,

how can a object refer to reference here!!! it is illegal argument
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

in the getweight(mineral m) method,
how can a object refer to reference here!!! it is illegal argument


Your question is not that clear. Can you please elaborate a bit? Did you mean Object here?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

First of all I don't think this code will compile because of line number 7 where the type of s is not specified.

Now coming to the question, if you see the call to the method getweight in the main method, there's a type cast there



So basically you type cast objects in a mineral array into Object type in the loop and then again type cast them to mineral to call the getweight method...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
actually!!! my doubt is

lets consider Mineral a class....during creation of object;

mineral m=new mineral();


is new mineral() and mineral m are same......... can we refer an object as mineral m ... i guess it is only a variable which is used to refer the object ......isn't it!!

so in the program if you see...o is type casted to type casted to mineral class first!!! and then it is passed as an argument in getweight((mineral)o) method in main method..... which is refered by the method in class miner which extends mineral!!!
which is public static void getweight(mineral m) ....

So my question now is that how can new mineral(); passed as an argument in main method .....will refer to the miner class getweight method which has getweight(0 method which takes an reference variable as an argument..... isnt it illegal argument error!!


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

so in the program if you see...o is type casted to type casted to mineral class first!!! and then it is passed as an argument in getweight((mineral)o) method in main method..... which is refered by the method in class miner which extends mineral!!!
which is public static void getweight(mineral m) ....

So my question now is that how can new mineral(); passed as an argument in main method .....will refer to the miner class getweight method which has getweight(0 method which takes an reference variable as an argument..... isnt it illegal argument error!!


I'm sorry. I read this more than three times... And I don't get what you are trying to say.

Are you saying that the cast shouldn't yield the type specified (compile time error), or are you trying to say that the type is wrong (and yield a runtime exception)?


Basically, I lose you here...

which is refered by the method in class miner which extends mineral!!!


What extends mineral? The train of thought on that sentence says that miner extends mineral -- which from the code, it doesn't. Or you could be refering to the object passed to the getWeight() method -- which it doesn't really make much sense either.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
ok!!

in the miner class if you see,there is a method named getweight(mineral m), which has a mineral class reference....ok

and in main method we call that method using getweight(new mineral());.....ok

my doubt is how can the arguments match in this case...... how can reference of the class and object of the class can we meaningfully same!!

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

how can reference of the class and object of the class can we meaningfully same!!


Reference points to objects...

Mineral m = new Mineral();

Here you are declaring a reference (named m) to Mineral -- and point it to a Mineral object created by the new operator.

In your example, the getWeight() method declares a reference (named m) to Mineral -- that will be point to the Mineral object that is passed in, which is one that is created by the new operator.

Henry
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
so,

you say that ....method(int i)

can be passed a argument as new method(Integer());
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40


The purpose of a reference is to point to objects -- that why they are called references (they refer to objects).

Primative types don't reference objects. So, technically, you can't have a primative int point to an object Integer. (let's keep autoboxing out of this discussion for the moment)

Henry
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
oh ok!!! better!! thanks henry
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
Hi,

For me, to make this discussion simple I would suggest Vijay to do some reading and research about the difference between: primitive and reference types, also the difference between an object, a class and its constructor and what's the function of each one of them.

In my case, I was so confused about this question that I didn't know where to start my answer.

Wish you best.

Cheers!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object reference