Jump to content

why final is been used in --> public static final System out

- - - - -

  • Please log in to reply
4 replies to this topic

#1
sharathm

sharathm

    Newbie

  • Members
  • Pip
  • 2 posts
why final is been used in --> public static final System out;

in the system class

#2
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
It's done like this so that you can't just simply change them. You must call setIn(), setOut(), etc, which can then validate your changes.

#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Actually final won't let you change it even through the setter method. It must be going around the restriction somehow. I was looking around in the Java source and managed to track the code until:
public static void setOut(PrintStream out) {
    checkIO();
    setOut0(out);
}

and then:
private static native void setOut0(PrintStream out);

i read that native keyword indicates that non-java code is being run from the method. No idea where it goes further....

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I suppose the final keyword is only checked at compile-time so avoidable at runtime using reflection or anything.

#5
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
One of the nice things about Java is that you don't have to know why it works, just that it does. :)

[JavaSpecialists 096] - Java 5 - "final" is not final anymore - A nice read.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users