Reputation: 53
I heard that in java Instance (non-static) methods work on objects and to invoke non static method requires to have a reference to an instance. But here in this Java(Android) code non Static method is called without creating an object inside onCreate() method and no errors. I wonder why is that?
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newGame();
}
private void newGame(){
// code here
}
}
sorry for my low knowledge in java
Upvotes: 0
Views: 1352
Reputation: 201447
There is an instance. Java objects are created with a constructor, since your MainActivity
didn't include one you got a default one. It looks like,
public MainActivity() {
super();
}
Then your onCreate()
is invoked on that instance.
Upvotes: 0
Reputation: 7641
It is because the method newGame() is the member method of your class/activity name MainActivity. According to OOP concepts, you don't need class object if you are calling member method of same class. It is same like member variable. That is what you want.
Upvotes: 2