+ 11

Can a class be static?

5th Sep 2018, 10:17 AM
Byk
13 Réponses
+ 7
classes cannot be static in c++ since c++ is not completely object oriented, the static keyword is used in a class to specify that it is not bound to objects of the class (independent data) and can be accessed without object references. moreover a class in c++ has no life span since it's s self-contained data structure and lives in a global scope, even though namespaces are used, it's just to avoid name conflicts and to encourage code organization. however in completely object oriented programming languages like java and c# were classes can be defined within another class, it's reasonable to make classes static since their scope is no longer global but depends on some other scope, even the main function is to be static since it's defined within a class, this is just to emulate the capabilities of a global type which lives for as long as the program runs. without making these classes and main static, they may go out of scope and this may cause problems you wouldn't want to occur 😉
9th Sep 2018, 6:25 AM
Germain F
Germain F - avatar
+ 6
Java, Yes 👍💗
8th Sep 2018, 11:09 PM
NimWing Yuan
NimWing Yuan - avatar
+ 5
No, it can't be. Outside of a class, the static keyword ¹ specifies the type of storage class ² . It can be used with variables. Inside of a class, the static indicates that the members of the class (data members and member functions) are not bound to class' instances, so they can be accessible and used without object creation. A class by itself has no storage requirement but instances of the class need to have it. _____ ¹ https://en.cppreference.com/w/cpp/language/static ² https://en.cppreference.com/w/cpp/language/storage_duration
5th Sep 2018, 2:14 PM
Babak
Babak - avatar
+ 4
Before this , you have to make yourself clear about what does "static" mean . Declearing something static ,mean it belongs to a class, and doesn't require any instance of it, to call it or execute it. So, a class can be declared static only if there are multiple nested classes under it (Since there must be a "Top Level Class" to which all static declared classes will belong), so that inner class , belongs to outer class directly & it doesn't require any reference to call it. But you cannot declare a Top level class static , since it voilates the meaning of static (it must belongs to a class) , as no further classes are present to which it can belong. This is the case in java ,Little I know about C++.
10th Sep 2018, 12:36 PM
Mr.Curious
Mr.Curious - avatar
+ 2
Su-bhat could you please elaborate ? I have never seen static class..
5th Sep 2018, 4:12 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Su-bhat oh.. I missed java tag in question.. I thought of c++ alone..
6th Sep 2018, 5:06 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Yes
8th Sep 2018, 6:59 PM
Rahul Sharma
+ 2
But remember you can only declare a static class inside a top-level class, it is illegal to declare it inside an inner class. Nested classes (a class within a class) are the only ones that can be declared static. This is so the parent class does not have to be instantiated to access the nested class. Can a top level class be static in Java? So, Yes, you can declare a class static in Java, provided the class is inside a top level class. Such classes are also known as nested classes and they can be declared static, but if you are thinking to make a top level class static in Java, then it's not allowedThe answer to this question is both Yes and No, depending on whether you are talking about a top level class or a nested class in Java. You cannot make a top level class static in Java, the compiler will not allow it, but you can make a nested class static in Java. A top level class is a class which is not inside another class. It may or may not be public i.e. you can have more than one class in a Java source file and only needs to be public, whose name must be same as the name of the file, rest of the class or interface on that file may or may not be public. On the other hand, a nested class is a class inside a top level class. It is also known as the inner class or member class in Java. Read more: https://javarevisited.blogspot.com/2017/04/can-we-declare-class-static-in-java.html#ixzz5R2TWMCm1
13th Sep 2018, 1:54 PM
deepak sharma
deepak sharma - avatar
+ 2
deepak sharma , I said that only.
17th Sep 2018, 9:10 AM
Mr.Curious
Mr.Curious - avatar
+ 1
The classes defined under top level class can be defined static..
5th Sep 2018, 10:23 AM
Su-bhat
Su-bhat - avatar
+ 1
Ketan Lalcheta My answer was pertaining to Java's inner classes, not C++.. Sorry if that was misleading.. Outer/top-level class cannot be static in Java too..
6th Sep 2018, 3:37 AM
Su-bhat
Su-bhat - avatar
0
of cause, a class can be static in Java, most likely if it's within another clas
9th Sep 2018, 12:51 PM
Akingbesote Jesulonimi
Akingbesote Jesulonimi - avatar
0
I would like to add that if you want that your class can never be instanciated by others classes and "force" the use of the declarated static methods you can declare the constructor as private. Then this class will be a "singleton" class.
9th Sep 2018, 5:42 PM
Akazukin Chacha
Akazukin Chacha - avatar