How to get a value in an object in Java? - java

Let's say I have something like this :
public class obj{
public int x;
public obj(int x){
this.x = x;
}
}
and this :
public class main{
obj o = new obj(1);
public static void main(String[] args){
//get the value of OBJ
//add 1 to the value got from above(let's just say I wanted to. no reason why)
}
}
is there a possible way to get the value of the obj's x value without changing the x to static? And make it so that you can change it? when I try to do something like this, it always says to change the x to static. why? I know this may be a weird question but i just wanted to know :)

You need to change your object to static first since you are accessing it directly from static main.
private static obj o = new obj(1);
// You can get the value of `x` from `o` as follows:
int x = o.x;
// You can increment the value in the object `o` as follows:
o.x++;

First of all, your class declaration is wrong. You don't use parenthesis in class declaration.
Also, in Java you write class names big. (The class Object already exists, don't make a class "Obj" because it would mean the same and is an abbreviation.)
public class MyObject {
public int x;
public MyObject(int x){
this.x = x;
}
}
public class Main {
MyObject myInstance = new MyObject(1);
public static void main(String[] args){
// You can just do ++ behind the variable. This adds +1.
myInstance.x++;
// If you want to get the value of the (public) variable x:
int temp = myInstance.x;
}
}

Yes you can get it by using
o.x
It is not telling you to change x to static. It is telling you to make o to static. o is non-static and cannot be used in the static main method.

Non-static instances can't be assessed in static method directly. Since o is an instance variable rather than static variable, you will need to create an instance of Main to access it's non-static variable.
public class Main {
Obj o = new Obj(1);
public static void main(String[] args){
Main main = new Main();
main.o.x += 1;
}
}

Related

why this.static_variable not giving error when this refer to instance variable not Class in java

class Test {
int instance_variable ;
static int static_variable = 15;
Test(int abc){
this.instance_variable = abc;
}
static void sget (){
System.out.println(static_variable);
}
void get(){
System.out.println(this.static_variable + " from static ");
System.out.println(instance_variable + " from instance ");
this.static_variable = 90;
}
}
public class ABC {
public static void main(String[] args) {
Test t1 = new Test(10);
Test t2 = new Test(13);
t1.get();
t2.get();
// Output
// 15 from static from obj
// 10 from instance from obj
// 90 from static from obj
// 13 from instance from obj
}
}
As you can see I am able to use this.static_variable.
I have debugged the program and I have seen this doesn't not contain static variable when how it is able to give value of static variable?
It should throw error on this line and I am also able to print a static variable from instance method void get .
There is no point of error in the above code.Static members always have global scope. Passing value to global variable using constructor is nothing wrong at all.Even not refering through this keyword will also yield the same result. 'this' keyword points to instance variable inshort global variable.So. the same definition of this will be maintained for static variable too.

How to build a method that increment the value of instance variable when it is called in main by an object?

I want to build a method that when it is called in main, automatically increment the value of variable.If first value is 0 when its called increment it to 1 , when I call for second time from 1 to be incremented at 2 and so on.
Notes: The method is part of a class and it called via object in main.
you can use the static variables. Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.
Create a static variable whose value can be updated in a function.
let this be your class
class Student {
static int b; //initialized to zero only when class is loaded not for each object created.
public void update(){
//incrementing static variable b
b++;
}
public void showData(){
System.out.println("Value of b = "+b);
}
}
and this be your main class
public class Demo{
public static void main(String args[]){
Student s1 = new Student();
s1.update();
s1.showData();
Student s2 = new Student();
s2.update();
s1.showData();
}
}
output:
Value of b = 1
Value of b = 2
Well, you kinda left a lot up for interpretation here, assuming the "value of variable" is part of the object the code would be something like this:
public class MyClass {
public int myVariable = 0;
public MyClass() {}
public void incrementMyVariable() {
myVariable++;
}
}
How you would handle this depends on where the variable your referring to is located ie. main method, object variable, etc... Additionally, if the variable needs to be kept constant across all the objects created for the class you would need it to be static.

Does the final keyword store variables inside methods like static?

Newbie to Java here. I'm in the process of porting my iPhone app to Android. From what I've read, the final keyword is pretty much equivalent to static. But does it work the same inside a method?
For example in Objective-C inside a method... static Class aClass = [[aClass alloc] init]; wouldn't be reallocated again and it wouldn't be disposed at the end of the method.
Would something in Java inside a method like... final Class aClass = new aClass(); act the same?
No. Block-local variables go out of scope when the block is exited and are logically (and usually physically) allocated on the stack.
Java isn't really like Objective C in that respect, and final is more akin to const because it indicates a reference may not be altered. In your example, when the block ends the final variable will be eligible for garbage-collection as it is no longer reachable. I think you want a field variable something like
static final aClass aField = new aClass();
Note that Java class names start with a capital letter by convention...
static final MyClass aField = new MyClass();
You are confusing the meaning of Final and Static.
Final means that the value of the variable cannot be changed after its value is initially declared.
Static means a variable can be accessed and changed without needing to instantiate a class beforehand.
Perhaps the following bit of code will make this more clear.
public class SF1 {
static int x;
final int y = 3;
static final int z = 5;
public static void main(String[] args) {
x = 1;
// works fine
SF1 classInstance = new SF1();
classInstance.y = 4;
// will give an error: "The final field main.y cannot be assigned"
z = 6;
// will give an error: "The final field main.z cannot be assigned"
reassignIntValue();
// x now equals 25, even if called from the main method
System.out.println(x);
}
public static void reassignIntValue() {
x = 25;
}
}
You have to declare your variable in class scope so you can able to access it outside of your method.
class ABC{
int a;
public void method(){
a = 10; // initialize your variable or do any operation you want.
}
public static void main(String args[]){
ABC abc = new ABC();
System.out.println(abc.a) // it will print a result
}
}

Can an instance of a class modify static / class variable?

class Person {
public static int age = 10;
}
public class Application {
public static void main(String[] args) {
Person p = new Person();
p.age = 100;
System.out.println(Person.age);
Person.age = 22;
System.out.println(p.age);
}
}
I got 100 and 22 printed. Was I wrong in assuming that instances of a class cannot access/modify class/static variables.
I think the part your confused by is the meaning of static. the age variable in class Person will be shared across all instances of Person, and can be accessed with no instance at all via:
Person.age = 100;
Changing it for any instance:
Person p = new Person();
p.age = 100;
changes it for everyone, and is the same as calling
Person.age = 100;
Changing it in a non static way, meaning via some instance only makes the code misleading by making people think they are changing an instance variable at first glance. You will get a compiler warning about this.
Yes, they can. From the docs:
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. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class
Of course an instance of a class can access and modify a static field, if it's accessible to that scope.
What cannot happen is a static statement/method body modifying an instance it does not "know about", e.g. a static method using this.
Example
public class Main {
private static int meh = 0;
int blah = 0;
public static void main(String[] args) {
// ugly but compiles fine
// equivalent to Main.meh = 1
meh = 1;
// Will NOT compile - main method is static,
// references instance field "blah" out of scope
// Equivalent to this.blah = 1
blah = 1;
}
}

Static methods and static fields in Java behave somewhat in a strange way

By convention, a static method specifically in Java can have access only to static fields or other static methods. The following simple code snippet however appears to violate the convention. Let's consider the following simple code snippet in Java.
class Super
{
protected static int x;
protected static int y;
public Super(int x, int y)
{
Super.x=x;
Super.y=y;
}
public static int sum()
{
return(x+y);
}
}
final class Sub extends Super
{
public static int temp=100;
public Sub(int x, int y)
{
super(x, y);
}
public void concreateMethod()
{
System.out.println("\nInstance variable x = "+x);
System.out.println("Instance variable y = "+y);
}
}
final public class Main
{
public static void main(String[] args)
{
Sub s=new Sub(10, 5);
System.out.println("\nAssociating with object x = "+s.x);
System.out.println("Associating with object y = "+s.y);
System.out.println("\nAssociating with class name x = "+Sub.x);
System.out.println("Associating with class name y = "+Sub.y);
System.out.println("\nSummation (Associating with object) = "+s.sum());
System.out.println("Summation (Associating with class name) = "+Sub.sum());
System.out.println("\nAssociating with class name temp = "+Sub.temp);
System.out.println("Associating with object temp = = "+s.temp);
System.out.println("\nConcreate method called.");
s.concreateMethod();
}
}
The above code produces the following output with the respective statements.
Associating with object x = 10
Associating with object y = 5
Associating with class name x = 10
Associating with class name y = 5
Summation (Associating with object) = 15
Summation (Associating with class name) = 15
Associating with class name temp = 100
Associating with object temp = = 100
Concreate method called.
Instance variable x = 10
Instance variable y = 5
The static fields s and x are being accessed through the following statements within the main() method using the object of the Sub class, though they are declared as static in the super class Super.
Sub s=new Sub(10, 5);
System.out.println("\nAssociating with object x = "+s.x);
System.out.println("Associating with object y = "+s.y);
The following statements of course, have no doubt.
System.out.println("\nAssociating with class name x = "+Sub.x);
System.out.println("Associating with class name y = "+Sub.y);
Since x and y are static, they can certainly be accessed in this way.
The same is the method call, observe the following statements.
Sub s=new Sub(10, 5);
System.out.println("\nSummation (Associating with object) = "+s.sum());
System.out.println("Summation (Associating with class name) = "+Sub.sum());
Both of the ways, the static method sum() is being accessed using the object of the class Super and also using the class name Sub.
Again the similar case with the static field temp declared within the Sub class
System.out.println("\nAssociating with class name temp = "+Sub.temp);
System.out.println("Associating with object temp = = "+s.temp);
The static field temp is being accessed in both the ways.
Why is this happening here?
Basically it's a flaw in the design of Java IMO which allows static members (methods and fields) to be referenced as if they were instance members. This can be very confusing in code like this:
Thread newThread = new Thread(runnable);
newThread.start();
newThread.sleep(1000);
That looks like it's sending the new thread to sleep, but it actually compiles down into code like this:
Thread newThread = new Thread(runnable);
newThread.start();
Thread.sleep(1000);
because sleep is a static method which only ever makes the current thread sleep.
Indeed, the variable isn't even checked for non-nullity (any more; it used to be, I believe):
Thread t = null;
t.sleep(1000);
Some IDEs can be configured to issue a warning or error for code like this - you shouldn't do it, as it hurts readability. (This is one of the flaws which was corrected by C#...)
There is no problem there. Static methods can only access static fields and call other static methods as you have stated. Nothing in your examples does otherwise.
Non-static methods can access both static and non-static methods and fields. Again, none of your examples violate that.
The Sub.temp and s.temp are equivalent and you can use both, it means the same. But 1st is better one because suggests it's a static field.
a static method specifically in Java can have access only to static fields or other static methods declared within the same class
Or its superclass.
I don't see any violation here, you can access static fields/methods via its concrete object or class name. both refer to the same thing.
Where do you see a non-static field or method being accessed by static code? Everything seems perfectly fine to me.
Perhaps what's confusing you is that static fields and methods can be accessed through instances as well as through the class name? It's certainly a big ugly and many consider it bad design, but that's all.

Categories