0
Constructors Within Methods
I'm trying to call a constructor in a method (the method performs an action on the object and returns a new object of that type with the changes made to it). I've already tried but my IDE underlines the second part of my constructor call (ie. for the constructor call `Object obj = new Object(int n);` `new Object(int n)` is underlined. Is this possible at all or am I just calling the constructor wrongly?
6 Respostas
0
Update: I just did a Google search on this question and apparently it's impossible. I'll just have to find another way to get what I want done.
Thanks for everyone's help (#more Google searches).
+ 1
Alright Souptik Nath. I'll try it and let you know if it works.
0
Can I see code or that part of code..?
Can't say with this info..
Edit:
When calling a method just pass value..
No need to put type...
0
You dont need to mention the data type while calling constructor.Declare 'int n' before calling constructor
Write the underlined line as this:
Object obj=new Object(n);
0
It looks like my code is horribly wrong. I'm going to rewrite it from the ground up. I'll come back here if I need any help.
Thanks!
0
Here's part of current code (I can't share the entire code 'cause it's an assignment). What I really want to do is create a second instance of the object so that my method here only affects the second instance and returns it.
//Think of the object as a square grid. This method is supposed create a new
//object and flip it's outer elements, returning the new grid.
void upsideDownFlip() {
this.size = n;
Squarelotron atarashi = new Squarelotron(n);
squarelotron [0] = squarelotron [5];
squarelotron [5] = Squarelotron.squarelotron [0];
squarelotron [1][0] = squarelotron [1][5];
squarelotron [2][0] = squarelotron [2][5];
squarelotron [3][0] = squarelotron [3][5];
squarelotron [4][0] = squarelotron [4][5];
for(int i = 0; i < squarelotron.length; i++) {
System.out.println(Arrays.toString(squarelotron[i]));
}
return;
}
Please ask for any necessary further information.