Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object reference

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,

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

can be passed a argument as new method(Integer());
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok!!! better!! thanks henry
 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic