Class which is to be called through the class object.
Code:
Code:
- public class NewMemoryAllocation {
- public void show()
- {
- System.out.println("NewMemoryAllocation Class !!!");
- }
- }
Main Class which will call the NewMemoryAllocation Class through its variable.
Code:
- public class NmaMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- NewMemoryAllocation nma = new NewMemoryAllocation();
- nma.show();
- }
- }
Without allocating memory to nma Class Variable, and trying to call the Class NewMemoryAllocation, would throw compilation error to initialize the Variable nma.
If nma variable gets initialized to null value and program is made to run, program will give run time error of "Null Pointer Exception". So, it's mandatory to allocate the memory to Class variable through 'new'.
No comments:
Post a Comment