Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to accept an instance of a class as a parameter

 
Nyah Bell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following is apart of an assignment (I changed the code to be brief) and I am not posting it for my work to be done for me - I am just extremely Lost.



I am just confused as to how to handle this. Do I use something from java.io library (Scanner)? How do I handle a parameter that is just a bunch of instances??

My apologies in advance for any confusion but I am at a loss. Any help is appreciated.

Thanks in Advance
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nyah, Welcome to JR

What do you want to do with the instance passed to the method? You can use it just like any other instance.

 
Nyah Bell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well - unlike your example - there are only constructors within class A and B, so there are no return values (this is what is confusing me)

I am trying to add code to this:



the code that I need to insert is to manipulate the parameter e that is passed

in my original post, inside of main, i created


I need to get to whats inside of the instances of A and B
(almost like your example of A.methodA but when attempting this ( I am using Netbeans IDE) it is not possible to access the variables that way
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Class A or Class B all the fields are public. So you can access them using <instanceName>.<fieldName>. In the example I provided I had made the fields are private and then provided Getters/Setters to access/manipulate the fields.
 
Nyah Bell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, so when it is not given a name but is just



and B is just a class with a constructor

that instance does not have a name, it is just sitting there on the right side - what would I put in the <instanceName> part? 'A' is being passed, so can I do something like:

A.B.method

??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nyah Bell wrote:
...
that instance does not have a name, it is just sitting there on the right side - what would I put in the <instanceName> part? 'A' is being passed, so can I do something like:

A.B.method


Am not able to understand what you are trying to do with the code you have provided in the above post. Firstly its not understandable and secondly its not syntactically correct.
 
Nyah Bell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay sorry,

I just did not want to post up my entire actual code but I will post my actual code and go from there.



All of that was given as the teacher's code - he has said not to change it because it should work fine and the only changes we should make is adding our code where he told us to add it (as noted with the comments)

I am expected to use a stack to bind variables, but before I can do so, I need to figure out how to get to those variables. For example, in main, Expr e = new Block(new . . . . . );
e is then passed as a parameter
What can I do within the eval method to get to the variables that were instantiated in e ("x", new Declaration("x"........) etc.?

It is of course perfectly okay if you cannot help because I definitely appreciate you trying to understand me (I can be confusing) and understand what I am trying to do. This is much appreciated.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the eval method-
Firstly you need to cast "e" to Block to be able to use Block class's instance fields. That way you would have to proceed to access other class's instance fields. And before casting try to add an instanceof check. The reason for casting is that- Which fields can be accessed is determined by the Type of the reference.
So if you have the type of reference as Expr and your type of instance is Block, to access the left field in the Block instance you need to have the Type of reference as "Block" and not "Expr". Because the association of the fields with the reference type is done at the compile time.
 
Nyah Bell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you SO much! Now I understand - yes! So by casting I am helping it understand what it is and what it can reach.

Thank you very very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic