Java only static members in main class - java

Is it bad practice to, in the class which contains the main method, declare all members of that class as static? If so, why? Is it better to, in the main method, create a new instance of the enclosing class, and run the program from the constructor so to speak?
EDIT: (clarification)
I know the concept about static and singletons and generally when to use it. But this question regards specifically the main-class of a program. #Andrew Tobilko (who apparently removed his answer..) seems to have best understood my question judging from his answer. In my case, my main-class is about 200 LOC, and it uses two other small class (< 100 LOC each), so it's a small program. The main-class contains creating a Swing GUI and some running logic, nothing which there needs to be several instances of, so I thought I might just make everything static, to be able to use everything from the static main-method. Is this motivated? A friend who codes in C# told me using a lot of static would mean death penalty in C#. Can there be some memory problems with it or something?

It's not 'better' - it depends on what you need to do.
Declaring all the members of a class as static (including methods) simply turns the class into a singleton. If that's your use-case, then yes.
Declaring an attribute as static means that there is only one copy of this attribute which is shared by all the instances of the class.
If your use-case requires creating multiple instances, and each one of these objects should have its own "private copy" of an attribute then you shouldn't declare that attribute as static.
It's recommended to read more about the topic before you continue implementing.

Static Variables:
The static key word is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.
Static variables are also known as class variables. Local variables cannot be declared static.
Static Methods:
The static key word is used to create methods that will exist independently of any instances created for the class.
Static methods do not use any instance variables of any object of the class they are defined in. Static methods take all the data from parameters and compute something from those parameters, with no reference to variables.
Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.
static modifier are for class members. It should be only used when you want to get a single copy of instance through out the program.
Below is example to explain it,
public class InstanceCounter {
private static int numInstances = 0;
protected static int getCount() {
return numInstances;
}
private static void addInstance() {
numInstances++;
}
InstanceCounter() {
InstanceCounter.addInstance();
}
public static void main(String[] arguments) {
System.out.println("Starting with " +
InstanceCounter.getCount() + " instances");
for (int i = 0; i < 500; ++i){
new InstanceCounter();
}
System.out.println("Created " +
InstanceCounter.getCount() + " instances");
}
}
This would produce the following result:
Started with 0 instances
Created 500 instances
You could check this DuplicateQuestion for your reference

Static entity can give a call to or interact with static entities i.e. a static method can call only a static method or interact with a static variable. That is the property of static methods. Main class in java is mostly declared as "public static void main", which makes main a static method. Hence, the functions it calls are to be static.

Generally class used for constants will hold all final static
variables.
Utility classes holds all static methods.Eg - Math
class in Java

Related

what does "static" mean and the difference of it between different languages(specifically C and Java)

I am currently learning Java and C and get confused with the "static" keyword. In C I mainly use static variables as global variables. In java, I use it for initializing methods eg.
"public static void SomeMethods(){}". also for global variables within classes
eg.
public class ThisClass{
static int var=0;
public static void main(){var++;}
public static void add(){var++}
}
what does static mean and whats the difference in java and C?
The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.
The static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class.
Variable :-The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc.
The static variable gets memory only once in the class area at the time of class loading.
Method :-A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
Block :-Is used to initialize the static data member.
It is executed before the main method at the time of classloading.
For C : - check this link https://www.javatpoint.com/static-in-c#:~:text=Static%20is%20a%20keyword%20used,variable%20is%20throughout%20the%20program.

Accessing variables from multiple methods in java

I'm starting to learn java, and I don't fully understand class variables and instance variables. I've seen some StackOverflow posts about accessing a variable from multiple methods, and most of the answers say to use static class variables. My question is, why can't one just use private instance variables? What are the advantages of using one over the other?
Instance variables are only accessible from instance functions (i. e. from non-static functions). Under the OOP paradigm, most of your variables will likely be instance variables.
Class variables (static) can also be accessed from class functions (static), like your main function.
Say, you have a class called Car. Every instance of this class (say, myFerrari and myBeetle) has its own variables doors, engine, gearbox et cetera. Thus, they are non-static, as are functions like drive (because you are driving an individual car, not the concept of cars).
But you might have a static variable MAX_ALLOWED_PASSENGERS which tells you how many seats a car may have before it's considered a bus instead. This is tied to the concept of cars (i. e. the class), not to any individual car. Thus, it might be static.
As a beginner, depending on your method of learning, you may not yet understand OOP and only write procedural code - which is not what Java is designed for. Thus you may encounter weirdnesses, like code examples where all functions and variables are static. This is a side effect of using Java in an unintended way and its purpose will be clear to you later on.
You can think of a static variable as a shared variable between all instances of the class. It is mostly used when you have a need of common value to be accessible across all objects of the class. Look at below example and execute it. It will clarify things. Please note that I have accessed a static variable with instance which is not the right practice but it is for demonstration purpose only.
public class A{
static int counter=1;
private int value;
public A(int value){
this.value = value;
}
public int getValue(){
return this.value;
}
}
public class B{
public static void main(String[] args){
A a1 = new A(10);
A a2 = new A(12);
System.out.println(a1.value);
System.out.println(a2.value);
System.out.println(A.counter);
System.out.println(a1.counter);
System.out.println(a2.counter);
A.counter=20;
System.out.println(A.counter);
System.out.println(a1.counter);
System.out.println(a2.counter);
}
}
Notice how changing value of counter in class A impacts the result for both variables.
Ok first of all you should in most cases use instance variable.
class variables (static variable) are used when you need one instance across the whole class.
meaning if you have 2 different objects the static variable will be used for both of them, while using instance variable means that each object will hold its own vriable with its own value.
example:
public class Test{
public static int classVar=1;
public int instanceVar;
}
now this code will yield different resulkt based on the variable used:
Test o1=new Test();
o1.instanceVar=1;
Test o2=new Test();
o2.instanceVar=2;
System.out.println("class var object 1"+o1.classVar);//will result in 1 and warn that shoud be used as class level there for as Test.classVar
System.out.println("instance var object 1"+o1.classVar);//will result in 1
System.out.println("class var object 1"+o2.classVar);//will result in 1
System.out.println("instance var object 1"+o2.classVar);//will result in 2

Why is it possible for objects to change the value of class variables?

By Oracle's definition,
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory.
By this definition, it is safe to deduce that a static variable belongs to the class and shouldn't be accessible for modification by any object of the class.Since all objects share it.
So this line from the same definition is a bit confusing:
Any object can change the value of a class variable...
So I tried this code and it prints 45 (although I get a warning saying "Static member accessed via instance reference"):
public class Main {
static int value = 8;
public static void main(String[] args) {
// write your code here
Main main = new Main();
main.value = 45;
System.out.println(value);
}
}
If this was a Student class, and I had a static variable called numberOfStudents, why should one object of that class be allowed to change the value of this class variable?
It's not really that "one object" can - it's just you're in code which has access to that variable, and unfortunately Java allows you to access static members (both variables and methods) as if they were instance members. This ends up with very misleading code, e.g.
Thread t = new Thread(...);
t.start();
t.sleep(1000);
The last line looks like it's asking the newly-started thread to sleep - but actually it'll make the current thread sleep.
This is basically a flaw in Java. The compiler will silently turn code like this into
Thread.sleep(1000);
or in your case
Main.value = 45;
(I believe that in an older version of Java, it would emit code that checked for nullity with the variable you were accessing the static member "through", but it doesn't even do that any more.)
Many IDEs will allow you to flag code like this with a warning or error. I would encourage you to turn on such a feature. If you see existing code like that, change it to use access the static member directly via the declaring class, so it's clear what's going on.
By this definition, it is safe to deduce that a static variable belongs to the class and shouldn't be accessible for modification by any object of the class.Since all objects share it.
No, static field is accessible for modifications, as long the access modifier allows it.
main.value = 45;
The compiler will read this line at compile-time as:
Main.value = 45;
Being able to create a class with static variables and methods so that those variables and methods are shared by all instances or objects created from the class can be very useful, see When to use static methods.
When sharing a static variable in a class between multiple instances or objects created from the class, the synchronized modifier may be required in order to ensure that if the static variable is being modified by objects in more than one thread, that data integrity is maintained, see What does synchronized mean? and also see How to synchronize a static variable among threads running different instances of a class in java.
The final key word, see How final keyword works is used to determine whether a variable is immutable or not. So if you want to have a class static variable that should be immutable or a constant then you can add the final modifier to the definition. However see Java final keyword for variables which explains that the underlying value for a reference may not be immutable in the sense that functional programming means. See also what is meant by immutable as well as Why final keyword is necessary for immutable class.
You can also use modifiers such as public to determine the visibility of variables and methods in a class, see What does public static void mean in Java.
By using modifiers such as final or private the programmer is able to finely tune the visibility and modifiability of variables in class and objects instantiated from the class.
Litle example how the compiler change the object field access to a class field access.
public class A {
static int foo = 25;
static public void main(String[] arg){
B b = new B();
A a = b;
System.out.println(b.foo);
System.out.println(a.foo);
}
}
class B extends A {
static int foo = 60;
}
The output is:
60
25
It also shows that can be confiusing as it have different behaviour as for object fields.
By this definition, it is safe to deduce that a static variable belongs to the class and shouldn't be accessible for modification by any object of the class.Since all objects share it.
No. By this definition, that static variable belongs to the class and is modifiable by any instance of the class. There is no implication that when some variable is shared that it should not be modifiable. Use final if you want that.
If this was a Student class, and I had a static variable called numberOfStudents, why should one object of that class be allowed to change the value of this class variable?
To increment the value in constructor and decrement it in finalizer, for example.
A static variable has a single instance for the whole class that defines it. When an instance is created, an instance of that static variable IS NOT CREATED. There is only one, and that one is freely modifiable by any function without the need for an instance. (unless it is declared final)

JAVA : Accessing static method properly

I am new to JAVA, and I like to try and understand everything.
When accessing a static method "hero.returnHp()" in JAVA, I have the following:
hero Mike = new hero();
Mike.returnHp();
The program runs fine, but I notice that Eclipse has a warning stating, "The static method from the type hero should be accessed in a static way." When I accept the auto-fix, it changes "Mike.returnHp();" to "hero.returnHp();".
So I have two questions:
1) What is the advantage of this?
2) If I created two objects of the same type, how would I specify which one to return when accessing in a static way?
Thanks!
I would first like to point out what the keyword static means.
Static variables only exist once per class – that is, if you create a class with a static variable then all instances of that class will share that one variable. Furthermore, if it’s a public static variable, then anyone can access the variable without having to first create an instance of that class – they just call Hero.staticVariableName;
Static method/functions are stateless. That is, they act only on information (1) provided by arguments passed to the method/function, or (2) in static variables (named above), or (3) that is hard-coded into the method/function (e.g. you create a static function to return “hello” – then “hello” is hard-coded into the function).
The reason why Eclipse wants you to access static methods in a static way is because it lets you and subsequent programmers see that the method you’re accessing is static (this helps to prevent mistakes). The function will run either way you do it, but the correct way to do it is to access static functions in a static way. Remember that if you call a static method, no matter what instance variable you call it from (Tim.returnHp, Jim.returnHp, Mike.returnHp, whatever) you will call the same function from the hero class and you will see the exact same behavior no matter who you call it from.
If you created two objects of the same type then you COULD NOT specify which one to return when accessing in a static way; static functions/methods will refer to the entire Hero class.
Can you explain what you’re trying to do so that we can offer more specific feedback? It’s quite possible that returnHp() shouldn’t be static.
Is that “return hit points”? If it is, then you do NOT want it static because the number of hit points that a hero has is part of the hero’s state, and static methods are stateless. (Think of state like the current condition – alive, dead, wounded, attacking, defending, some combination of the aforementioned, etc.) I would recommend going into the Hero class and changing returnHp to a non-static method.
Now… I know you didn’t ask, but I would like to advise you of something:
Class names (such as Hero) should be capitalized. Instance variable names (such as mike) should be lowercase. This is a widely accepted naming convention and it will increase the readability of your code.
Jeff
A static method is one which belongs to a class but not to an object. In your example above, you have created an object Mike of class hero. The method returnHp() is static, and belongs to the hero class, not the hero objects (such as Mike).
You will likely get an IDE or compiler warning when you reference a static method from an object, because it should never be tied to that object, only to its class.
Based on the method name, I would guess it shouldn't be static.
class hero {
private float hp;
public float returnHp() { // Should NOT be "public static float ..."
return hp;
}
}
The JavaDocs on class members has a brief discussion on statics as well. You may want to check that out.
A static method is completely independent of any instances of the class.
Consider that this works, and does not result in a NullPointerException:
hero Mike = null;
Mike.returnHp();
(by the way, class names should start with a capital, and variable names be lowercased).
Here is another neat example: Being a static method, Thread.sleep always sleeps the current thread, even if you try to call it on another thread instance.
The static method should be called by class name, not through an instance, because otherwise it is very confusing, mostly because there is no dynamic dispatch as static methods cannot be overridden in subclasses:
hero Tim = new superhero(); // superhero extends hero
Tim.returnHp(); // still calls the method in hero, not in superhero
You are getting a compiler warning now, but many people say that this was a design mistake and should be an error.
It is part of the JVM spec.
You don't need to. A static method is common between instances of a class, your confusion arises from thinking it is an instance method.
static means a static way. One reason to use static is you can access it using class directly. that is its benefit. that is why main is always static. The entrance function don't need to create an instance first.
Actually if you search static in google, and understand it deeply. U will know when and why use static.

Local variables in java

I went through local variables and class variables concept.
But I had stuck at a doubt
" Why is it so that we cannot declare local variables as static " ?
For e.g
Suppose we have a play( ) function :
void play( )
{
static int i=5;
System.out.println(i);
}
It gives me error in eclipse : Illegal modifier for parameter i;
I had this doubt because of the following concepts I have read :
Variables inside method : scope is local i.e within that method.
When variable is declared as static , it is present for the entire class i.e not to particular object.
Please could anyone help me out to clarify the concept.
Thanks.
Because the scope of the local variables is limited to the surrounding block. That's why they cannot be referred to (neither statically, nor non-statically), from other classes or methods.
Wikipedia says about static local variables (in C++ for example):
Static local variables are declared inside a function, just like automatic local variables. They have the same scope as normal local variables, differing only in "storage duration": whatever values the function puts into static local variables during one call will still be present when the function is called again.
That doesn't exist in Java. And in my opinion - for the better.
Java doesn't have static variables like C. Instead, since every method has a class (or instance of a class) associated with it, the persistent scoped variables are best stored at that level (e.g., as private or static private fields). The only real difference is that other methods in the same class can refer to them; since all those methods are constrained to a single file anyway, it's not a big problem in practice.
Static members (variables, functions, etc.) serve to allow callers of the class, whether they're within the class or outside of the class, to execute functions and utilize variables without referring to a specific instance of the class. Because of this, the concept of a "static local" doesn't make sense, as there would be no way for a caller outside of the function to refer to the variable (since it's local to that function).
There are some languages (VB.NET, for example), that have a concept of "static" local variables, though the term "static" is inconsistently used in this scenario; VB.NET static local variables are more like hidden instance variables, where subsequent calls on the same instance will have the previous value intact. For example
Public Class Foo
Public Sub Bar()
Static i As Integer
i = i + 1
Console.WriteLine(i)
End Sub
End Class
...
Dim f As New Foo()
Dim f2 as New Foo()
f.Bar() // Prints "1"
f.Bar() // Prints "2"
f2.Bar() // Prints "1"
So, as you can see, the keyword "static" is not used in the conventional OO meaning here, as it's still specific to a particular instance of Foo.
Because this behavior can be confusing (or, at the very least, unintuitive), other languages like Java and C# are less flexible when it comes to variable declarations. Depending on how you want it to behave, you should declare your variable either as an instance variable or a static/class variable:
If you'd like the variable to exist beyond the scope of the function but be particular to a single instance of the class (like VB.NET does), then create an instance variable:
public class Foo
{
private int bar;
public void Bar()
{
bar++;
System.out.println(bar);
}
}
If you want it to be accessible to all instances of the class (or even without an instance), make it static:
public class Foo
{
private static int bar;
public static void Bar()
{
bar++;
System.out.println(bar);
}
}
(Note that I made Bar() static in the last example, but there is no reason that it has to be.)

Categories