Java - static methods and static variable - java

I have a question that I can't answer
which one is NOT true!
satic methods can access to static variable and static methods
static methods can access to non-static variable and non-static methods
non-static methods can access to static variable
non-static methods can access to static methods

static methods can access to static variable and static methods :- true
Reason :-class level fields/methods can access the other class level fields/methods
static methods can access to non-static variable and non-static method :- false
Reason :-class level fields/methods can not access the object level fields/methods as there is a probability object has not been created yet
non-static methods can access to static variable :- true
Reason :-Object level fields/methods can access the class level fields/methods as its logical
non-static methods can access to static methods :- true
Reason :-Object level fields/methods can access the class level fields/methods as its logical

2nd is partially wrong.
Static method can't directly access non static method. But, they can access non static methods on an object corresponding to the methods.

2 is FALSE Because at the time you call a static method it is not guaranteed, that the instance (non-static) variables/functions exist.

No 2 is not True.
Static methods can only access static members and variables.
Non-static methods can access both static and non-static members and variables.

Related

Use of this and super within local classes defined in a static context

I was going through the following code while studying Java local classes :-
class A {
protected int one;
}
class Outer {
static void staticMethod(){
class InnerLocal extends A {
double first = this.one;
double second = super.one;
}
}
}
My doubt is that when we declare the local classes inner to static method or static initializer block then they implicitly work as static member classes as they need no outer class to instantiate them. However I know the difference clearly between a static member class and static local class(i.e. the inner class defined in static block), the problem is that Java does not allows the object references 'this' and 'super' to be used in static context but the code above compiles perfectly.
Can anyone please provide me the reason why Java compiler doesn't complains on using 'this' and 'super' in static context in the above case? Thanks!:)
My doubt is that when we declare the local classes inner to static method or static initializer block then they implicitly work as static member classes as they need no outer class to instantiate them
This is the root cause of your confusion. Just because a method is static does not mean that anything declared within the method is also static. If you declare a variable (say int a) inside a static method, it is said to be a local variable. It would make no sense to say that a is a method local static variable. Similarly, it would make no sense to say that InnerLocal is a method local static class. (There is no such thing as static local variable in Java)
InnerLocal is therefore a regular class that extends from A and inherits the a member variable from A and is able to access it either through this or through super. It would also help to know that final is the only non-access modifier that can be used within a method in Java.
Java does not allows the object references this and super to be used in static context but the code above compiles perfectly.
static as in "static class" is not the same as static as in "static context".
All instance methods and constructors have access to this and super. Since constructors have access to this and super, initializers of instance fields have access to this and super as well. A static inner class has no outer instance, but it has its own instance. That is what this refers to.
In contrast, static methods have no access to this and super, regardless of the class in which these static methods are defined (top-level, static inner, non-static inner, anonymous, etc.)

Invoke a non static method from a static method

Can anyone please explain me why it is illegal for static method to invoke a non static method?
A non-static method is an instance method which acts upon a specific instance (object), and has access to that object's members. A static method is not associated with a specific instance.
It is legal to call a non-static method if you provide an instance:
class Foo
{
public static void f()
{
instanceMethod(); // Not OK - no instance provided.
this.instanceMethod(); // Not OK - no "this" in static methods.
new Foo().instanceMethod(); // OK because you provide an instance
}
public void instanceMethod() {}
}
because to call static method you don't need object of class, where to call non static method you need,
Static method is associated with class where non static method is associated with state (Object)
A non-static method is a method that executes in the context of an instance . Without an instance it makes no sense to call one, so the compiler prevents you from doing so - ie it's illegal.
If a method doesn't access any instance fields (aka instance variables) then it should probably be changed to be a static method.
non static methods have the implicit parameter this which static methods have not (because there is no this)
Alone because of this fact it would not be possible to call non-static methods form static ones.
Static methods belong to the class. Non static methods belong to an instance.
The non static method require an instance (this) to be executed on. The static method don't require any instance, since the class itself is the instance to which it belongs.
If I have 100 object instance, every nonStatic() method invocation affects the state of a different object. So every nonStatic method is 'different' because it acts on different entities.
At the same time, even if I have 100 instance of an object, calling staticMethod() doesn't affect any of those instances, because it can only affect the static members of the class.
There is 1 class and 1 instance of every static methods or static member variables.
For the non static members we have every single method associated to the class instance.
So if you would be able to call the nonStatic method, from a static method, which of the 100 instance would be affected? You see: it doesn't make sense.
Static methods are connected with the class.
Regular methods are connected to the object.
In static method there is no object on which you can run the non-static method.
It is not illegal to call a non-static method from a static method. Whenever you invoke a static method there is no need of an instance of a class whereas in case of non-static case you do.

What is the scope of variables declared inside a static block in java?

Are variables declared inside a static block accessible anywhere else?
What "kind" of member are they(ie., are they static member, too?)
Generally programmers don't need to declare any variables inside static blocks, usually this is only for ensuring initialization of static variables for use by all instances of class (depending on scope of static variable).
Variables declared inside a static block will be local to that block just like methods and constructors variables.
JDK Docs
Variables declared inside a block are accessible only inside of that block. Static or no.
Variables declared inside a static method are static. They can only access other static variables or global variables.
they are like method variables. Only accessible in the block scope.
No, not visible outside the block. They act like local variables -- think of a static block as an anonymous function that gets called at class initialization. They are not static members.

Java: Accessing Static Methods

Can a non-static method be accessed from a static method?
And can a static method be accessed from a non-static method?
A static method can be accessed from a non-static, but not the other way around. If you're inside a static method, you can only access a non-static method if you have an instance of an object on which to call the method.
Can a non-static method be accessed from a static method?
No because an object is required to access non-static method and there are chances that an object is not available while accessing a static method
can a static method be accessed from a non-static method?
Yes.
To access a non-static method, you need to have an instance of an object. If you are inside a static method, then you are not able to call non-static method, because, per definition, you are not "inside" an object (so you don't have any object to call this method on). You can however call non-static methods from static if you have a reference to the object you want to call the non-static method on.

java objects, shared variables

I have a simple question here.
If I declare a variable inside an object which was made [declared] in the main class, like this:
public static int number;
(
usually I declare it like this :
private int number;
)
can it be used in a different object which was also made [declared] in the main class?
btw I do not care about security atm, I just want to make something work, don't care about protection)
Here's a telling quote from Java Language Specification:
JLS 8.3.1.1 static Fields
If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized.
A field that is not declared static (sometimes called a non-static field) is called an instance variable. Whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.
[Example program follows...]
In short, a static field is a class variable: it belongs to the class, as opposed to the instances of the class. In a way, you can think of a static field as a variable shared by instances of the class, but it's much more consistent to think of static fields as belonging to the class, just like static method also belongs to the class, etc.
Since they belong to the class, they do not require an instance of said class to access (assuming adequate visibility), and in fact it's considered bad programming practice to access static members through an instance instead of a type expression.
Related questions
Java - static methods best practices
Static methods
calling non-static method in static method in Java
non-static variable cannot be referenced from a static context (java)
When NOT to use the static keyword in Java?
Static variables and methods
If the class holding 'number' is called MyClass
you can refer to it as MyClass.number from any method.
Doing so for a variable is not good design though.
There are really two issues here: public vs. private in the context of inner classes, and static variables.
Part 1:
static means that you don't need an instance of the class to access that variable. Suppose you have some code like:
class MyClass {
public static String message = "Hello, World!";
}
You can access the property this way:
System.out.println(MyClass.message);
If you remove the static keyword, you would instead do:
System.out.println(new MyClass().message);
You are accessing the property in the context of an instance, which is a copy of the class created by the new keyword.
Part 2:
If you define two classes in the same java file, one of them must be an inner class. An inner class can have a static keyword, just like a property. If static, it can be used separately. If not-static, it can only be used in the context of a class instance.
Ex:
class MyClass {
public static class InnerClass {
}
}
Then you can do:
new MyClass.InnerClass();
Without the 'static', you would need:
new MyClass().new InnerClass(); //I think
If an inner class is static, it can only access static properties from the outer class. If the inner class is non-static, it can access any property. An inner class doesn't respect the rules of public, protected, or private. So the following is legal:
class MyClass {
private String message;
private class InnerClass {
public InnerClass() {
System.out.println(message);
}
}
}
If the inner class has keyword static, this would not work, since message is not static.
static variables are shared by all instances of a given class. If it's public, it is visible to everything.
non-static variables belong to only one instance.
Since your main method is static, it can only see static variables. But you should avoid working statically - make an instance of a class, and pass the data around as method/constructor parameters, rather than sharing it via static variables.

Categories

Resources