permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Effects from using static class members from a final class, in another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Effects from using static class members from a final class, in another class" Watch "Effects from using static class members from a final class, in another class" New topic
Author

Effects from using static class members from a final class, in another class

Herland Cid
Greenhorn

Joined: Feb 28, 2013
Posts: 6
Hi. I am looking forward to use static class members from a final class, in a normal class. Is there a way this can be implemented directly?

Here is my attempt. I instantiate the static class member, I want to use it directly (following the principle that says, static class members will exist independently of any class instance).

Any lights on this, is there a close point that could equal my attempt? Here it is:



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

First of all it doesn't matter that the class is final. Using one of its static members has nothing to do with whether the class can be extended (which is what final is about).

And don't create an instance of the class for the purpose of accessing the static member. It's true that Java allows you to do that, but it's unnecessary and confusing, so don't do it that way. Just use the ordinary way where you use the class to access the member:

Herland Cid
Greenhorn

Joined: Feb 28, 2013
Posts: 6
Thank you Paul!
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi Herland ,

As Paul said you can use the static members directly by using class name.
If you declare any member (Either variable or method) as static then you can access that member using class name. But in your code you are trying to access the member by creating an object instead use class name it will save memory.

Regards,
James
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Effects from using static class members from a final class, in another class