Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is Final class a static class ?  RSS feed

 
Ketung Xiao
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I see code: x_final_class_name.x_method_name which implies x_final_class_name is a static class.

 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Please show us real example code that displays what you mean.
 
Ketung Xiao
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Code:



LocalTime API signature:

public final class LocalTime
extends Object
implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable
 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What method are you referring to, LocalTime.now()?

LocalTime is a final class. now() is a static method.

I don't know how that call implies that LocalTime is static. In fact, it can't be, because only members can be static, and LocalTime is a top level class.
 
Paul Clapham
Sheriff
Posts: 22277
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A final class can have both static methods and instance methods. In fact LocalTime has both static methods and instance methods. Clearly we can't deduce from that information that LocalTime is a static class. Which, as Stephan rightly points out, it isn't.
 
Roel De Nijs
Sheriff
Posts: 11302
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ketung Xiao wrote:x_final_class_name.x_method_name which implies x_final_class_name is a static class.

That's impossible to tell with the provided information! It could be, but most likely it is not. Most classes are top-level classes and if a class is a top-level class (like LocalTime), it can't be declared as static (you'll get a compiler error if you try to do so). Only a nested class can be static, but it's not required to be final.
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final class is not static class. Final means clas can't be overridden and static means class nested in another class. Top-level public class can't be static.
Static classes can be good for utility static methods when you want to access it without creating instance of outer class.
 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spencers wrote:and static means class nested in another class

Not exactly. There are two types of nested classes: Static nested classes and inner classes. You can have a nested class that is not static.
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static and Final both are different topics.

static is an optional specifier which allows our variables in JAVA program to behave as variables in C program(assuming if you already have an experience in C programming). When you create an object using "new" operator you are actually calling a complete new instance of that class where by default all the variables are defaulted to their respective default values.(ex- int =0, string =null, etc.). The "static" keyword allows us to escape this behavior. When initialize a static int variable say a=5, throughout the class and its instances a will 5, till the program has finished execution.

final is an optional specifier which allows you to create constants behavior in your JAVA program. When you assign a variable to a particular value and make it as final the meaning of that variable never changes till the doomsday 

Hence the difference which lies between the above two is you can modify static variable in your program but you cannot modify a final variable.
The above behaviors apply to fields , methods, classes, interfaces, everywhere in java.
 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Stark wrote:static is an optional specifier which allows our variables in JAVA program to behave as variables in C program(assuming if you already have an experience in C programming).

This is NOT true. There is no equivalent behavior, because C doesn't have classes.

When initialize a static int variable say a=5, throughout the class and its instances a will 5, till the program has finished execution.

This is also NOT true. You can reassign static variables. The new value will be visible to all code that has access to the variable.
 
Roel De Nijs
Sheriff
Posts: 11302
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Stark wrote:When initialize a static int variable say a=5, throughout the class and its instances a will 5, till the program has finished execution.

That's incorrect! A static variable can be reassigned (unless it's marked final as well). And that's easy to illustrate with a code snippet
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the last 2 statements, i have clearly mentioned that static variables can be modified!!!

Yes. C programming does not have classes! In C programming when you initialize a variable that stays as it is until you modify it later ((metaphorically) law of inertia ). C programming does not have static blocks like java does, that is different but the behavior of static variables in Java and variables in C are comparable. Behavior of both are same(again metaphorically same  ).
 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Stark wrote:Please read the last 2 statements, i have clearly mentioned that static variables can be modified!!!

That means your post contradicted itself, leading to more confusion.

the behavior of static variables in Java and variables in C are comparable

In what way are they comparable that doesn't apply to Java's instance fields?
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it doesn't contradicts my statement. Because I said the values you assign to static variables will remain same till the program ends until you assign it to some other value.

When I said comparable I meant only static variables in Java not instance fields. I guess I have clearly mentioned this in my previous reply.
 
Stephan van Hulst
Saloon Keeper
Posts: 7504
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Stark wrote:When I said comparable I meant only static variables in Java not instance fields.

Yes, and I'm asking you to clarify how they are comparable.
 
Roel De Nijs
Sheriff
Posts: 11302
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Stark wrote:No it doesn't contradicts my statement. Because I said the values you assign to static variables will remain same till the program ends until you assign it to some other value.

Hmm, first of all that's not what you initially have said. Secondly that's a pretty confusing statement... will the value assigned to a static variable remain same till program ends and/or until another value is assigned?
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh my good god! Quicksand! Quicksand! Quicksand!
Now let me make this straight guys.


When you initialize a static variable in Java with any value then that variable retains the value until you by yourself change it. If you don't change it the same value will be retained for that variable till the program ends.


Now the same behavior in C. When you declare and use a global variable, that variable will be assigned by you with some value. Any value. You use that value throughout the program , you might change or reassign or you might not. If you change the updated value will be maintained or else the first initialization will be used throughout the program.


The same thing applies to local variables in C, difference is the life span for local variables are till the method execution completes.

In all the above 3 scenarios we are discussing about static variables only! I think now I might have made myself clear. There are no instance variables in here just static. By default all variables in C are static because there is no concept of instances in C, thats obvious!!! By this I think I have justified that static variables in Java and variables in C are similar or comparable. 😅😋
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ketung Xiao , I guess I have cleared at least your doubts regarding static and final... 😋
 
Ketung Xiao
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for all the replies.
So, Java allow static method access of a final class.

I'm like the blind man int the story of 'blind men and the elephant'.
Since I don't have the whole picture of the Java Specification, I had made an illogical conclusion that a final class is also a static class.

But I still don't quite understand why top level static class is not allowed.  Wouldn't be good to put all the helper classes in a top level static class
and hence make methods in top level class available to other class without instantiating this 'top level' static class ?
Does it has to do with class constructor issue ? All Java classes must have a constructor, right ?
It would not make sense to have a constructor in top level static class (which is not allowed).




 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your doubt is partially correct. Java does not allow to make a class as static. The reason is all classes (user defined and system provided) inherit from the super parent class called Object. You might have heard about 'toString' method of String class, it actually hails from Object class and String extends Object.

For statics the concepts like instances, parent field v/s child field are all completely inapplicable. If you extend a class having a static method then the implementation and method resolution at the runtime is completely different as that of for the instance methods. For instance methods the overridden definition is selected but for static class specific definition is selected. All this cannot be achieved if the class is marked as static.

I am not 100% sure about your doubt regarding constructor but yes that is also one of the reason.

And the your point regarding helper classes at the top level, I strongly suggest you to go through factory design pattern that will clear your doubt 😀😊
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your expected behaviour you can consider marking the class as final rather than static. Which inturn will lead you to design pattern only 😋
 
Roel De Nijs
Sheriff
Posts: 11302
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ketung Xiao wrote:But I still don't quite understand why top level static class is not allowed.  Wouldn't be good to put all the helper classes in a top level static class
and hence make methods in top level class available to other class without instantiating this 'top level' static class ?
Does it has to do with class constructor issue ? All Java classes must have a constructor, right ?
It would not make sense to have a constructor in top level static class (which is not allowed). 

I think your confusion is caused by a wrong understanding about what can be done with a static class and how static nested/inner classes differ from regular nested/inner classes. It seems that you think that a static class can't be instantiated, but that's completely incorrect! Let's see if I can clear your confusion with some code examples and a bit of explanation.

Let's start with a simple top-level classFrom this class you can easily create an instance and invoke its methods. For example

Now let's create a utility class so methods can be invoked without the need of creating an instance firstAnd you can (as promised in the intro) invoke the sayHi() method without creating an instance of the Greetings class In fact, the no-arg constructor is made private so it's impossible to create an instance of this class. Here's a code exampleAs you can see the Greetings class is (just like the Person class) a top-level class and there was no need to make it static (as this is not allowed and would result in a compiler error) to invoke the methods of this class without creating an instance of this class. This class is marked as final, but that's not a requirement. Most of the utility classes are marked final as there is rarely the need to subclass a utility class. But even without marking final the above code snippets will compile successfully.

Now let's proceed with a code example of a regular nested (inner) classProbably the main characteristic of a regular nested (inner) class is that you must have an instance of the outer class (Car in this example) to be able to instantiate the inner class (Engine in this example). And here's another illustrative code snippetOn line1 and line2 an Engine instance is being created using a Car instance (I know, the syntax looks a bit strange ). On line3 we try to create an Engine instance without a Car instance, but this doesn't work and the compiler provides the following error message: "No enclosing instance of type Car is accessible. Must qualify the allocation with an enclosing instance of type Car (e.g. x.new A() where x is an instance of Car)". So that's exactly what I've mentioned as the main characteristic of a regular nested (inner) class. Please note that the syntax on line2 and line3 (to create an Engine instance) are almost identical! So two paranthesis can make the difference between a successful compilation or a compiler failure

Now let's see what happens if we turn Engine into a static nested (inner) classSo that's was very easy: simply add static to the class declaration. Now let's see which impact this minor change has to the above main() methodNow line1 and line2 result in the (same) compiler error: "Illegal enclosing instance specification for type Car.Engine". On the other hand, line3 compiles successfully and creates an Engine instance without the need of a Car instance. So it's very similar to a static method: to invoke a static method you don't need an instance of the class. To create an instance of a static nested (inner) class, you don't need an instance of the outer class. And you can also create instance of any top-level class without the need of an instance of another class. So that's why a static top-level class makes no sense (and results in a compiler error).

And finally, if you wonder why you would need such a static nested (inner) class, it's often used in the Builder pattern as illustrated in this code snippet

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 11302
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacky Stark wrote:Java does not allow to make a class as static.

And what about static nested classes?

Jacky Stark wrote:The reason is all classes (user defined and system provided) inherit from the super parent class called Object. You might have heard about 'toString' method of String class, it actually hails from Object class and String extends Object.

That's complete nonsense! A static nested class will extend from Object as well and therefore inherit all its (visible) methods (like toString()).
 
Jacky Stark
Ranch Hand
Posts: 45
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oopsy! Static nested classes, totally missed out there! Thank you for repairing my parachute, it had become a knapsack!  
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!