Constructors Within Methods | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

24th Mar 2020, 6:07 PM
Jezaniah Apraku
Jezaniah Apraku - avatar
6 Answers
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).
6th Apr 2020, 6:53 PM
Jezaniah Apraku
Jezaniah Apraku - avatar
+ 1
Alright Souptik Nath. I'll try it and let you know if it works.
25th Mar 2020, 6:38 AM
Jezaniah Apraku
Jezaniah Apraku - avatar
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...
24th Mar 2020, 6:25 PM
Jayakrishna 🇮🇳
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);
25th Mar 2020, 5:48 AM
Souptik Nath
Souptik Nath - avatar
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!
2nd Apr 2020, 7:55 AM
Jezaniah Apraku
Jezaniah Apraku - avatar
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.
6th Apr 2020, 11:58 AM
Jezaniah Apraku
Jezaniah Apraku - avatar