what is the difference? with static and without [duplicate] - java

This question already has answers here:
Static enum vs. Non-static enum [duplicate]
(4 answers)
Closed 7 years ago.
public class TypeMessage {
public static enum GRAPH_ERROR {
ERROR_INPUT, ERROR_GRAPH
}
public enum INPUT_TYPE {
INTEGER, DOUBLE
}
}
First enum is static, second is not static. I use TypeMessage.GRAPH_ERROR and TypeMessage.INPUT_TYPE.
Q: Does the fact that I wrote the word static in this situation has no effect?

From Java Language Specification 8.9:
A nested enum type is implicitly static.
so there is no difference between them.

For enums it doesn't matter. You can access both enums in the same way: TypeMessage.GAPH_ERROR and Typemessage.INPUT_TYPE.

Related

"Which variable(instance or local) is in action in the public method of foo class?" [duplicate]

This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Is "this" shadowing a good idea?
(10 answers)
Closed 3 years ago.
It seems like its using local variable of the method!!!! I thought it would give an error on call non static variable on static method but it did not.
public class foo{
int x = 12;
public static void go(final int x){
System.out.println(x);
}
}
Actually it doesn't have a error which is interesting..
System.out.println(x)
will print the parameter of the method go
If you want to access field of the class, use this keyworld (if your method not static):
System.out.println(this.x)
In your case, you need to have an instance of foo class and use
foo f = new foo();
System.out.println(f.x);

Actual parameters in Java [duplicate]

This question already has answers here:
Java Pass Method as Parameter
(17 answers)
Closed 4 years ago.
I'm new to Java and I'm confused about actual parameters in Java. Since it's just parameters, I assume that literals, constants,variables, expressions can be actual parameters during function calling.
But I'm confused about a function call. Is it possible to have a function call being an actual parameter in Java?
If your question is "are functions first-class citizens in Java, as they are in C" then the question is "no, not really".
But with Java 8's lambdas, you can get pretty close:
import java.util.function.IntFunction;
public class Main {
public static void main(String[] args) {
final IntFunction square = x -> x * x;
printFunctionResult(square, 2);
}
public static void printFunctionResult(final IntFunction function, final int value) {
System.out.println(function.apply(value));
}
}

Static Variables in Java [duplicate]

This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
What is the difference between referencing a field by class and calling a field by object?
(5 answers)
Closed 5 years ago.
I know that static variables are part of class and not part of the Object . How can the following lines of code work without any problem
class M
{
static int i=0;
void Inc()
{
System.out.println("Global "+M.i);
System.out.println("Local "+this.i);
}
}
public class StaticTest
{
public static void main(String args[])
{
M m1=new M();
m1.i=99; //How can the m1 object access i variable of the class
m1.Inc();
}
}
The output i get is
Global 99
Local 99
How can the m1 object access i variable of the class?
It is the very same i variable in both cases.
unfortunately, java allows you to access static fields using non-static syntax.
That is all there is to this, nothing else behind this.
Yes, it is allowed for non-static members to access and update static members.
See this for more information here

How are constructor calls decided in Java? [duplicate]

This question already has answers here:
How does Java choose which constructor to use?
(2 answers)
Closed 6 years ago.
I have a Java class where there are two parameterized constructors
public class TestApplication {
TestApplication(Object o)
{
System.out.println("Object");
}
TestApplication(double[] darray)
{
System.out.println("Array");
}
public static void main(String[] args)
{
new TestApplication(null);
}
}
When I run this program I get output as "Array". Why does the Object constructor not run?
Constructor overloading resolution behaves the same as method overloading resolution. When two constructors match the passed parameter, the one with the more specific argument types is chosen.
null can be assigned to both a double[] and an Object, since both are reference types, but an array is a more specific type than Object (since arrays are sub-classes of the Object class), so new TestApplication(null) triggers the TestApplication(double[] darray) constructor.

How to define a constant creator method in Java [duplicate]

This question already has answers here:
How to declare a constant in Java?
(3 answers)
Closed 7 years ago.
How can I define a constant in my class that returns a object? What I'm looking for is something like BigInteger.ZERO
This is how BigInteger.ZERO is defined :
public static final BigInteger ZERO = new BigInteger(new int[0], 0);
public class SomeClass {
public static final type CONST_NAME = new SomeObject();
...
}
Then you can do SomeClass.CONST_NAME

Categories