package com.codegym.task.task06.task0617;
/*
Notepad for new ideas
*/
public class Solution {
public static class Idea {
public String getDescription()
{
return "sourav";
}
}
public static void printIdea(Idea idea)
{
System.out.println(idea.getDescription());
}
public static void main(String[] args) {
printIdea(new Idea());
}
//write your code here
}
this worked but need improvement
Under discussion
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
17 January 2019, 14:31
looks good to me
0
Gugun
11 May 2019, 14:10
I don't understand why the argument of printIdea method in main method is (new Idea())?
0
Guadalupe Gagnon
11 May 2019, 19:44
because the printIdea method takes an idea object. You can do it this way:
Idea temp = new Idea();
printIdea(temp);
But if you look at that code, temp equals new Idea(), you could save a whole line of code by not declaring the separate variable and passing it to the method and just do what the OP did.
0
Gugun
11 May 2019, 23:33
Thank you for the answer, it's really help. But I think I still having a question. Is there any difference between Idea temp = new Idea(); and
new Idea();?
Because if I write Idea temp = new Idea(); then Idea object has a reference, which is temp, while if I just write new Idea(); then Idea object doesn't have reference.
0
Guadalupe Gagnon
12 May 2019, 04:12
You are correct. In the code above there is not a need for a reference, but a lot of times there is a need for one. I will do it both ways depending on that need for the reference. Basically if you don't need to modify the data then there is no need for a reference. A good example of a need to change data is changing a string if it contains a certain value. Take for example this:
or
The second example there is a reference to the string and the code can modify that reference. The first one you just cant do it. Its a small example but i think you get the point. +1
Gugun
12 May 2019, 06:10
I think I get the point. Thank you for the explanation. :)
0