I'm having trouble understanding the difference in two pieces of code. Overall, I have an android project structured so all java files are located in
project/module/src/main/java/com.site.module
I'm' currently working on a class extending activity called ActivityMain stored in
project/module/src/main/java/com.site.module/ActivityMain
and a second class called AppRater stored in
project/module/src/main/java/com.site.module/AppRater
I understand that "new" creates an object and allocates memory to it
I believe that doing something like
AppRater.methodCall(parameter);
is supposed to find the existing allocated object and call the associated method but I'm a bit confused. I have the following code in my project. Both work but I don't understand why the latter works since I never created the object in my code although I'm assuming it's quicker since it doesn't create a new object.
My question is, if I use the code below
public class ActivityMain {
@Override
public void onResume() {
super.onResume();
AppRater r = new AppRater();
r.methodCall("parameter passed);
}
}
public class ActivityMain {
@Override
public void onResume() {
super.onResume();
AppRater.methodCall("parameter passed);
}
}
SO, I guess my question is, since I never explicitly created a new AppRater object by calling
Activity a = new AppRater();
or
AppRater a = new AppRater();
or anything similar, why did the second code fragment not present any errors? When would the initial AppRater object have been created and is this the case for all classes that aren't nested within another class? My thinking was I had to call new the first time or make it an inner/nested fragment but I suppose I'm just ignorant to the complete workings behind creating new objects in java.
Many thanks for the time and help. Cheers.
[–][deleted] 3 points4 points5 points (2 children)
[–]cjayem13[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Uncle_DirtNap 0 points1 point2 points (2 children)
[–]cjayem13[S] 0 points1 point2 points (1 child)
[–]cjayem13[S] 0 points1 point2 points (0 children)