Java: Access public member from a non-related class? - java

The following Java code is a stripped down example of the code I need. My question is, how do I access someInt from inside class Second? Note that Second implements another class, so I can not just pass someInt in.
package test1;
public class First {
public int someInt;
public static void main(String[] args) {
First x = new First();
}
public First(){
someInt = 9;
Second y = new Second();
}
}
class Second implements xyz{
public Second(){}
public void doSomething(){
someInt = 10; // On this line, the question lies.
System.out.println(someInt);
}
}

You can't access First's someInt field in Second because Second isn't an inner class of First. The changes below would fix your problem.
package test1;
public class First {
public int someInt;
public static void main(String[] args) {
First x = new First();
}
public First(){
someInt = 9;
Second y = new Second();
}
class Second {
public Second(){
someInt = 10;
System.out.println(someInt);
}
}
}

If you need to access the field in First (and not create a new one in Second), you need to pass a reference to the instance of First when you create the instance of Second.
Second y = new Second(this);
}
}
class Second {
public Second(First f){
f.someInt = 10;
In the terms of your question, "Access public member from a non-related class", the problem is solved by creating a relation. If that isn't allowed, this answer is wrong.

Accessing a public member follows the same syntax rules as accessing a public method (just without the brackets)
But having a public member in a class is usually not a good idea

The most direct way would be to
1) instantiate First
First f = new First()
2) access it directly because you made the instance variable someInt public
f.someInt = 10
A better way would be to provide accessors for someInt in First, and do it that way.
First f = new First();
f.setSomeInt( 10 );
...
int x = f.getSomeInt();

Within your second class, you must have a first class object. Create that object in your second class, then you will be able to access someInt.

You need to get a reference to an instance of First since someInt is not static.

Related

Java Inheritance issue with static and non-static context

Please help me I am facing bit problem in Java code.
I am not able to understand how to fix the error.
Please help.
public class A {
private int a = 100;
public void setA(int value) {
a = value;
}
public int getA() {
return a;
}
}
public class B extends A {
private int a = 222;
public static void main(String[] args) {
System.out.println("in main(): ");
a = 123;
System.out.println("a = "+super.a );
}
}
The error I get is:
int a in class Main must be static
First of all, you should tell us the error :).
It looks like you are trying to access a variable in a non-static context from a static context (main method is static).
You should do something like below:
public class B extends A {
public static void main(String[] args) {
B b = new B();
b.setA(123)
System.out.println("a = " + b.getA());
}
}
It doesn't make sense to declare another 'a' variable in the child class. If you want to access 'a' directly, you can declare the field in class A as protected.
First of all, just to be clear prior to going to the code, your 2 classes, given they are both public, must be in their own separate files.
Now let's go to your code. The error lies first in this statements inside your main method:
a = 123;
You are accessing B's instance variable a from a static context -this is one.
Second:
System.out.println("a = "+super.a );
A's instance variable a is never inherited by B because it has a private access modifier.
If you want to access A's a, you could create an instance of A, and use that to call the getA() method which returns the value of A's a
Cheers,

How would I access an int variable from one class to another? (Java)

For example:
In Class One
int killcount = 0;
In Class Two
killcount = 5;
All I want to do I get the variable from one class to another class. How would I do that?
Before trying to work with Bukkit I'd recommend you to get some Java experience first. That's not meant as an insult, but it can get quite confusing if you do it the other way round. Anyways, if you still want to know the answer to your question:
You'll have to create a getter & setter for your "killcount" variable.
class Xyz {
private int killcount;
public void setKillcount(int killcount) {
this.killcount = killcount;
}
public int getKillcount() {
return this.killcount;
}
}
Of course this is a simplified version without checks, but if you want to access the variable from a different class you can create an instance and use the methods to modify it.
public void someMethod() {
Xyz instance = new Xyz();
instance.setKillcount(instance.getKillcount() + 1);
//this would increase the current killcount by one.
}
Keep in mind that you'll have to use the same instance of the class if you want to keep your values, as creating a new one will reset them to default. Therefore, you might want to define it as a private variable too.
Consider the examples
public class Test {
public int x = 0;
}
This variable x can be accessed in another class like
public class Test2 {
public void method() {
int y = new Test().x;
// Test.x (if the variable is declared static)
}
}
Ideally, the instance variables are made private and getter methods are exposed to access them
public class Test {
private int x = "test";
public int getX() {
return x;
}
public void setX(int y) {
x = y;
}
}

Sending my current object to another class

Sorry if the wording of the title isn't correct. Say I have a class and I have initialized an object of that class, now in the constructor for that class I want to pass that new object's values to another class, is there a way to do this?
Example:
public class testinger
{
public static void main(String[] args)
{
prep ab = new prep(10);
}
}
class prep
{
private int a;
prep(int x)
{
a = x;
complete tim = new complete(/*how to send my current prep object there?*/);
}
public int getA()
{
return a;
}
}
class complete
{
complete(prep in)
{
in.getA();
}
}
You can refer to the current instance using the this keyword.
prep(int x)
{
a = x;
complete tim = new complete(this);
}
use the the keyword complete tim = new complete(this);
For example in Java this is a keyword. It can be used inside the Method or constructor of Class. It(this) works as a reference to the current Object whose Method or constructor is being invoked. The this keyword can be used to refer to any member of the current object from within an instance Method or a constructor
read more details here

Getting access to variable inside function

I am positive this has probably been answered many many times but I do not know the words to search for to find the answer. So here is the question
I'm using java and I have
int my_var = 3;
thing.myListener(new Listener() {
public void onStart(int posistion) {
my_var <-- I want to get access to my_var
}
});
How do I get access to my_var inside the onStart function. Also what is this type of problem called? Thanks!
You have to make it final. This is an anonymous inner class.
final int my_var = 3;
I would suggest using global variables. Here is an example:
A simple way to utilize "global" variables in Java is to define a class Global with the desired variables as static members of it:
public class Global {
public static int x = 37;
public static String s = "aaa";
}
Such members can be accessed by saying:
public class test {
public static void main(String args[])
{
Global.x = Global.x + 100;
Global.s = "bbb";
}
}
Is that what you are looking for?

java linked list problem

I have written some Java code with 3 simple classes where the first, Controller, has the main method and creates the instances of the other classes. Floaters is a classes that creates a linked list of Floater instances, each with a particular length and boolean value to say if they are vertical or not. My problem, as it says in the commented lines of the first class, is that both "humans" and "otters" Floaters instances are getting assigned the same values and thus have the same size....
Any suggestions on how to fix this?
Thanks in advance!
public class Controller{
private static Floaters humans;
private static Floaters otters;
public static void main(String[] args)
{
otters = new Floaters();
humans = new Floaters();
otters.addFloater(2, true);
otters.addFloater(3, true);
//this should read "2" and it does
System.out.println(otters.size());
//this should read "0" but reads "2". Why?
//How can I get it to read "0"?
System.out.println(humans.size());
}
}
import java.util.LinkedList;
public class Floaters {
private static LinkedList<Floater> llf;
Floaters()
{
llf = new LinkedList<Floater>();
}
public void addFloater(int length, boolean is_vertical)
{
Floater floater = new Floater(is_vertical, (byte)length);
llf.add(floater);
}
public int size()
{
return llf.size();
}
}
public class Floater {
int length;
boolean is_vertical;
Floater(boolean is_vertical, int length)
{
this.length = length;
this.is_vertical = is_vertical;
}
}
The llf in your Floaters-class is static. When you make variables static, they're linked to the class rather than the instance, and thus both instances of Floaters use the same list.
To correct this, simply remove the static from your declaration of the variable.
in floaters, llf should NOT be static
Because of static:
private static LinkedList<Floater> llf;
In this case static means a class field, shared among all instances of a class.
For example - mathematic functions in Java are declared as static metohods of the class java.lang.Math, matemathematical constants are static atributes of this class. So if you use sin(x), you are using always the same method.

Categories