Can non-static methods modify static variables - java

I am wondering how a non static method can modify a static variable. I know that static methods can only access other static methods and static variables. However, is the other side true? Can non-static methods access only non-static variables? For example:
public class SampleClass {
private static int currentCount = 0;
public SampleClass() {
currentCount++;
}
public void increaseCount() {
currentCount++;
}
}
This code compiles and I would like to know why in terms of static access privledges.

I have found this from The Java Tutorials
Instance methods can access instance variables and instance methods directly.
Instance methods can access class variables and class methods directly.
Class methods can access class variables and class methods directly.
Class methods cannot access instance variables or instance methods directly—they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for this to refer to.
So the answer is yes, non-static methods CAN modify static variables

No, any non-static method has access to static members. The only way this would be false is if the non-static context did not have access to the static member (ex. the static member is private to a class and the non-static code is not in that class). static variables exist to provide an instance free variable/method, so for example if we have a Game class and a highscore variable, the highscore would be static (accessible without an instance), and after every game (an instance of the Game class) completes we could alter the highscore from our non-static context if our score is greater than the high score.

Non static methods can access static variables.
Static methods can access only static variables or methods directly without creating object.ex:public static void main(String arg[])

Non-Static Methods can access both Static Variables & Static Methods as they Members of Class
Demo Code
public class Static_Class {
protected static String str;
private static int runningLoop;
static{
str = "Static Block";
}
/**
* Non-Static Method Accessing Static Member
*/
public void modifyStaticMember(){
str = "Non-Static Method";
}
/**
* Non-Static Method invoking Static Method
*/
public void invokeStaticMethod(){
String[] args = {};
if(runningLoop == 0){
runningLoop++;
main(args);
}
//Exiting as it will lead to java.lang.StackOverflowError
System.exit(0);
}
public static void main(String[] args) {
Static_Class instance = new Static_Class();
System.out.println(str);
instance.modifyStaticMember();
// Changed Value persists
System.out.println(str);
//Invoking Static Method
instance.invokeStaticMethod();
}
}

Look at it this way. A static variable can be accessed in many ways. One of the most common is to precede the var name with the class name, since static vars are per class.
Since you refer to this variable in the same class, you are exempt from having to precede it with the class name. It does not matter where you call the static variable.
Also this is a private static var not accessible by any other class.

Static variables are class variable not instance or local variable . that is why we can use static variable in non static method also. and static variables are not per object . static variables have one copy that will be used in entire program.

Static methods cannot modify Non-static fields since - For using a Non-Static field (outside the class) you must instantiate a class object,
But for using a Static method there is no need for object instantiation at all.
This is why it's not reasonable for a Non-Static Method (which not demands an object instantiation)
to modify a field that should be instantiated.
For this - Static methods can touch only static fields (or call other static methods).
But as you mentioned the other way around is possible,
A Non-Static method can modify a static field which is static for all objects of its class.

Static members are not instance members , these are shared by class , so basically any instance method can access these static members .

Yes, a static method can access a non-static variable. This is done by creating an object to the class and accessing the variable through the object. In the below example main is a static method which accesses variable a which is a non-static variable.
demo code:
public class Sample {
private int a;
void method()
{
System.out.println("i am a private method");
}
public static void main(String[] args)
{
Sample sample=new Sample();
sample.a=10;
System.out.println(sample.a);
}
}

Related

java- "new" operator in static method

I know this is reference variable which holds the reference for object and static method loads when the class is first accessed by class loader, either to create an instance, or to access a static method or field.
Why its not allowed for this but for instance itself? For e.g.
Class A{
public static void main(String... args){
this.method(); //not allowed
new A().method(); //allowed, how?
}
void method(){
System.out.println("Class A");
}
}
this.method(); //not allowed
You have no instance this in static context so you can't invoke method.
new A().method(); //allowed, how?
You have instance created by new operator so that you can invoke a method.
To invoke a method without having a real instance you have to declare it as static. i.e.:
static void method(){
System.out.println(“Class A”);
}
this will work when calling just method() and via instance:
public class A {
public static void main(String[] argv) {
method(); //works because method is static
new A().method(); //still ok, we can call static method using an instance
this.method(); //not allowed, there is no 'this' in static context
}
static void method(){
System.out.println("Class A");
}
}
static variables and instance variables follow the initialization order you mention when they are initialized in their declarations. Also static initializer blocks follow that order.
Methods (including constructors) on the other hand need no initialization at runtime. They are fully defined at compile-time and therefore exist and can be called as soon as the class is loaded, also before the initialization is complete. Therefore there is no problem in your main method instantiating the class (calling the default constructor) and calling a method that is declared later, as method is declared below main.
In addition, the main method is also only executed after any static initialization is complete.

Instance methods can access instance methods directly. What does this mean?

After going through Java documentation I understand that a field declared with 'static' keyword is class variable (or static field) and similarly when using 'static' during method declaration, is a static method (or class method). Class variable and Class methods are reference by class name itself.
Read this and this topics however could not understand following sentences from Java documentation.
What does it mean when it says.....
Not all combinations of instance and class variables and methods are allowed:
Instance methods can access instance variables and instance methods
directly.
Instance methods can access class variables and class methods
directly.
Class methods can access class variables and class methods directly.
The Java doc clearly explains about Class variables and Class methods but above 3 points are confusing to me.
Example Execution
1.Instance methods can access instance variables and instance methods directly.
Some Info:
Instance method are methods which require an object of its class to be
created before it can be called.To invoke a instance method, we have to
create an Object of the class in within which it defined.
Instance variables are declared in a class, but outside a method, constructor
or any block.
class Example1 {
int a = 100; // instance variable
public void printData() { // this an instance method called using an object of class Example1
getData(); // accessing another instance method directly without any object
}
public void getData() {
System.out.println(a); // accessing instance variable 'a' directly without any object
}
}
public class InstanceDemo {
public static void main(String[] args) {
Example1 obj1 = new Example1();
obj1.printData(); // will print 100
}
}
2.Instance methods can access class variables and class methods directly.
Class variables also known as static variables are declared with the static
keyword in a class, but outside a method, constructor or a block.
class Example2 {
static int a = 20; // 'static' / class variable
public void printData() { // instance method
setData(); // accessing class method directly
System.out.println(a); // accessing class variable directly
}
public static void setData() { // class method 'static'
a = 200; // setting value of class variable 'a'
}
}
public class InstanceDemo {
public static void main(String[] args) {
Example2 obj2 = new Example2();
obj2.printData(); // will print 200
}
}
3.Class methods can access class variables and class methods directly.
Static methods are the methods in Java that can be called without creating an
object of class. They are referenced by the class name itself or reference to
the Object of that class.
Memory Allocation: They are stored in Permanent Generation space of heap as
they are associated to the class in which they reside not to the objects of
that class. But their local variables and the passed argument(s) to them are
stored in the stack. Since they belong to the class so they can be called to
without creating the object of the class.
class Example3 {
static int a = 300;
public static void printData() {
getData(); // accessing class method
}
public static void getData() {
System.out.println(a); // accessing class variable 'a'
}
}
public class InstanceDemo {
public static void main(String[] args) {
//calling class method : class method are called using class name.
Example3.printData(); // will print 300
}
}
I think you just need to practice programming, so you can experience how the language works. Understanding the quirks of static access is one of the hardest things to grasp initially. Static members are generally geared towards providing class utility.
An instance method gets a pointer to the instance it's operating on, on the stack, therefore it can access the members of the instance. A static method isn't associated with any particular instance. There's no 'this' reference, thus it can't access instance members.
Instance methods can access instance variables and instance methods
directly.
This means a method that doesn't have a static modifier i.e. an instance method can access any non-static variable as well as call any non-static method.
Instance methods can access class variables and class methods
directly.
This means a method that doesn't have a static modifier i.e. an instance method can access a static variable or call a method with the static modifier.
Class methods can access class variables and class methods directly.
This means a static method can access any static variable as well as call any other method that has a static modifier.
To put it simply, an instance method i.e. a method without the static modifier can access both a static variable as well as a non-static variable and it can also directly call a static method and a non-static method.
On the other hand, a static method can only access static variables and call static methods. it cannot, however, access an instance variable or instance methods directly without an object.

Why can't we use 'this' keyword in a static method

class Sub {
static int y;
public static void foo() {
this.y = 10;
}
}
I understand that this represents the object invoking the method and that static methods are not bound to any object. But in the above mentioned case, the variable y is also static.
If we can invoke static method on class object, why can't we allow static methods to set the static variables of the class.
What is the purpose of this additional constraint?
Because this refers to the object instance. There is no object instance in a call of a static method. But of course you can access your static field (only the static ones!). Just use
class Sub {
static int y;
public static void foo() {
y = 10;
}
}
If you want to make sure you get the static field y and not some local variable with the same name, use the class name to specify:
class Sub {
static int y;
public static void foo(int y) {
Sub.y = y;
}
}
The main reason why we can not use "this" in static method context:-
this :- "this" means current class OBJECT , so its clear that "this" only come in
the picture once we intended to create an Object of that class.
static method:- there is no need to create an object in order to use static method.
means "instance" or object creation doesn't any sense with "static" as per Java rule.
So There would be contradiction,if we use both together(static and this) .
That is the reason we can not use "this" in static method.
this is referring to this instance of the object Sub. As the method is static, there is not an instance of Sub.
To make your code work write it like this:
class Sub {
static int y;
public static void foo() {
Sub.y = 10;
}
}
You can set static fields in static methods, but you don't have access to this in static method because this represents the current instance of the object, and in a static method you have no instance.
This means "this" object but there isn't one. In your case you can use the class name as #tibtof suggests.
"this" keyword is only applicable where an instance of an object is created. And in static method no instance is created because static method belongs to class area.
There is no problem with static methods setting values for static fields.
The only issue is usage of this keyword. Please note that since static methods are processed at the time of class loading, it's all but certain that no "this" exists at the point of time, which is why its only logical the usage of this keyword isn't allowed in a static context.
On the other hand, static method can be invoked from an object because it is made accessible to the object. The intention behind static data members and behaviours is to make it common to all the instances of that class.
Keyword "this" refers to the object that you are operation with. In your case this inside any non-static methods or constructor (if you have one and and if you use "this" inside that), then "this" refers to that particular instance of the class Sub.So it is applicable only when the object is created. But anything in the static context of a class, you can use without even creating object for that as it is resolved during the class loading. "this" resolved only when object is created ( you can even say dynamically for which object). So "this" make sense in static context. Hope it helps. God bless.
when we declare variable and method is static then this is share by among object where this keyword only pointing to current object. suppose you have created five object of class foo then only one copy of made of (int y) shred by all object.so if you access int y using this keyword then compiler get a ambiguity which object have to point because static int y is shared by all object . you have access static variable using class name.

Java anonymous class and reachable private variable

interface Test {
public void test();
}
public class TestMain {
private String h = "AAA";
public static void main(String[] args) {
TestMain t = new TestMain();
}
public TestMain() {
Test t = new Test() {
public void test() {
System.out.println( h );
}
};
t.test();
}
}
The above source works well.
But I think the variable 'h' should be unreachable from the anonymous class.
I need to know why it works well.
Thank you for all of your help in advance!
Each instance of a non-static inner class has an enclosing instance -- an instance of the outer class which it tied to it via a reference variable stored in the inner class object. All the members of the enclosing instance of available to the inner class object via that reference.
The compiler takes care of adding that reference variable, of course, as well as some special accessor functions that the inner class object can use to get to the outer class object's private members. You can see these accessor functions by disassembling the outer class using javap -c.
The basic rule for determining accessibility to private members is, according to section 6.6.1 of the Java Language Specification:
[I]f the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class that encloses the declaration of the member or constructor.
A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

Problems understanding static modifier

I am having difficulty understanding what a "static" method and "static" variable is and it is causing me problems with my code. Here is the code I am having difficulty with:
public class Document{
public void NewBlank(){
Resources.openRawResource(R.raw.blank);
}
}
Why do I get the error "Cannot make a static reference to the non-static method Resource.openRawResource(int) from the type Resources"? Why can't I reference a non-static method?
openRawResources is not a static method, it needs to be invoked in an object, not a type. In order to get an instance of Resources you could call getResources in an activity. Then the resulting code would be
Resources resources = myactivity.getResources();
resources.openRawResource(R.raw.blank);
A static method/variable is one that belongs to the class type, and not to the instances/objects of such type.
Cannot make a static reference to the non-static method
This means that for invoking that method you are trying to, you need a reference to an instance of that class.
Here's an example illustrating the difference:
public class Foo{
public static int staticVariable = 5;
public static void methodStatic(){}
public void nonStaticMethod(){}
}
here's how you can use them:
Foo.nonStaticMethod(); //can call static method referring to the class itself without having an instance
Foo f = new Foo();
f.nonStaticMethod(); //you need an instance of a Foo class in order to call a non-static method
For what concern static variables, these are variables that doesn't belong to a single instance of a class, but are shared between all different instances of the same class:
Foo a = new Foo();
Foo b = new Foo();
System.out.println(a.staticVariable); //print 5
System.out.println(b.staticVariable); //print 5
a.staticVariable = 10;
System.out.println(b.staticVariable); //print 10
(Please, look at the example above just to understand the concept of what a static variable is. You'll get the warning "access a static field in a non-static way" because that's not a proper way to access those variables)

Categories