0

I was searching answer for finalize method made protected in object class, I came accross the answer and finally got confused as i found For finalize() method of object class it has been said that : It's not public (or default access) because it's meant to be called by the JVM internally when the object is garbage collected - it's not meant to be called by anything else.

public static void main(String arr[]), this also meant to be called by JVM only, because JVM is outside my class outside my project, so to access this method by JVM, it should be public.

This make sense for JVM and main(), but finalize() method for the same reason as it is called by JVM, it can't be public.

Could someone please make this picture clear for me. Thanks in advance

Noorus Khan
  • 1,342
  • 3
  • 15
  • 33
  • There's nothing special about `main()`, it can be (and sometimes is) invoked by other code. Whereas invoking a finaliser manually is potentially dangerous. – biziclop Aug 04 '15 at 13:14

2 Answers2

1

There is no reason to prevent plain Java code from calling the main method; in fact, it commonly is called by framework code which executes the main method of an arbitrary class after performing some initialization.

As opposed to that, calling finalize of any object yourself is dangerous and is almost guaranteed to break the object's invariants.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • the question is why the finalize() can also not be public ? why is the reason given that it is meant to called by JVM so it is Protected, as such JVM is outside our project, how it can access protected feild @Marko Topolnik – Noorus Khan Aug 04 '15 at 13:28
  • @Max The JVM can do anything. `public/protected/private` only restricts user code. – biziclop Aug 04 '15 at 13:41
  • @biziclop Thanks, now I understand OP. Sure, the JVM can call any method. User code can also do it, the same way: over reflection. – Marko Topolnik Aug 04 '15 at 13:43
  • but the signature of main method is public static void main(string arr[]), and the reason for making it a public is always given and written everywhere that JVM is outside the project and JVM has to call main method thatswhy it is public, if JVM can do anything public/protected/private, then it means if we change in the JVM specification the main method as private, then, will we be able to call the main methos still ? @Marko Topolnik @ biziclop – Noorus Khan Aug 04 '15 at 20:56
  • Yes, we will. It's a one-liner to call any private method of any class and you don't have to be "the JVM" to do it. I don't remember seeing anywhere that "the JVM is outside the project" is given as justification for `main` to be public, but wherever you read that, it's a "just so" story, nothing more. Maybe you'll be surprised to know that the class containing the `main` method does not have to be public at all. It can be a package-private class. – Marko Topolnik Aug 04 '15 at 21:22
0

The answer is there are times when you may want to call the main method of another project from within your Java code. The same does not hold true for finalize

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80