I'm new to Selenium java and currently learning object reference and returns
I was trying to code this logic wherein I declare a non static variable in main class, declare an object for it, use that object in another class and return it to main class
My problem is when i return it back to main class it outputs 0. Is this scenario possible? or am I doing something wrong
package ReturnType;
public class Class1 {
public int Mul(int i, int j) {
Class2 cc = new Class2();
cc.c=i*j;
return cc.c;
}
}
Here is my main class
package ReturnType;
public class Class2 {
public int c;
public static void main(String[] args) {
Class1 xp = new Class1();
Class2 cc = new Class2();
xp.Mul(2,3);
System.out.println(cc.c);
}
}
Almost got the result... the return of the method is getting lost, you have to assign it to something
public class Class2 {
public int c;
public static void main(String[] args) {
Class1 xp = new Class1();
Class2 cc = new Class2();
cc.c = xp.Mul(2,3); //here assign it
System.out.println(cc.c);
}
}
Related
So say I have 3 java classes, A, B, and C. Class A has an int variable named num that equals 3. I want to be able to change num from class B to another number (for example, 45), then access that variable from class C so that it equals 45. How would I do this?
//First Class
public class A
{
public int num;
public A() {
num = 3;
}
public void setNum(int newNum) {
num = newNum;
}
public int getNum() {
return num;
}
}
//Second Class
public class B
{
public static void main(String[] args) {
A a = new A();
C c = new C();
System.out.println(a.getNum());
a.setNum(45);
System.out.println(a.getNum());
c.printStuff();
}
}
//Third Class
public class C
{
public void printStuff() {
A a = new A();
System.out.println(a.getNum());
}
}
The first two print statements output the expected 3 and 45, but the print statement in the C class still outputs a 3, even though I changed num's value to 45 in class B.
The reason you cann't get the result you want, is that you have created a new instance of class A in class C, and it has its own value, which is 3. Changing the value of the instance of class A which is defined in class B will not effect this new instance.
Note that you called the new method on class A two times so you have two seperate instances.
You can try this code to get the result you want:
//First Class
class A
{
public int num;
public A() {
num = 3;
}
public void setNum(int newNum) {
num = newNum;
}
public int getNum() {
return num;
}
}
//Second Class
public class B
{
public static void main(String[] args) {
A a = new A();
C c = new C();
System.out.println(a.getNum());
a.setNum(45);
System.out.println(a.getNum());
c.printStuff(a);
}
}
//Third Class
class C
{
public void printStuff(A a) {
//A a = new A();
System.out.println(a.getNum());
}
}
Thats happens because in C class you are creating a new instance of A again. So the value would be 3. You can change your constructor and call it like this,
public A(int value) {
num = value;
}
new Post(45);
By this when you create a instance you can assign a value to num as you wish from any class.
This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 5 years ago.
class ClassB {
int c=0;
public static void main(String [] args) {
ClassA cla = new ClassA();
c=cla.getValue();
}
}
class ClassA {
int value = 0;
public int getValue() {
ClassA obj=new ClassA();
return obj.value;
}
}
I want to 'int value' of ClassA in 'int c' of class B. The above code shows the error "non static variable c cannot be referred from a static context". Please provide the correct coding for me as I am stuck.
Of course, becuase you can't call a non-static method without instantiating the object that contains this method fisrt, either make all the methods and fields static, or instantiate the class:
class ClassB {
static int c=0;
public static void main(String [] args) {
ClassB clb = new ClassB();
c= clb.getvalueA();
}
public int getvalueA(){
ClassA cla = new ClassA();
return cla.getValue();
}
}
class ClassA {
int value = 0;
public int getValue() {
return value;
}
}
Variable c in class B is not static and main block is static block that's why it showing error.You can not reference non static field from static context.
public class Main {
public static void main(String[] args) {
int retVal;
TestClass a = new TestClass(20);
retVal = a.value;
System.out.println("Value of a: "+retVal);
}
}
class TestClass {
int value;
public TestClass(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
I want to use the the thread created on (say) instance x of class A in another class B.
I've stated my problems in better way in the form of comments below.
I have something like this:
Class A implements Runnable{
public static int num;
public void setNum(int i) { num = i; }
public int getNum() { return num; }
public void run(){
while(true){} //I want to keep this thread running continuously
}
}
Class B{
A a;
//I will call this method in class C to use the same instance of class A
public A getInstanceOfA() { return a; }
public static void main(String[] args){
a = new A();
Thread t = new Thread(a);
t.start();
a.setNum(5);
System.out.println(a.getNum()); //getting output as 5. Okay as Expected.
}
}
class C{
A a;
public static void main(String[] args){
a = getInstanceOfA();
System.out.println(a.getNum());
//Here I'm getting output 0 not 5 why? As Thread created on instance a is
//already running, and also I am using the same instance of class A
//so I should get the updated value 5, but getting 0. Why it is re-initializing num?
}
}
Please Help. Thanks.
I am not giving you the exact code but you should do it something like this.
Class B{
A a;
public B(){
initialize();
}
//I will call this method in class C to use the same instance of class A
public A getInstanceOfA() { return a; }
// this method should not be main
public void initialize{
a = new A();
Thread t = new Thread(a);
t.start();
a.setNum(5);
System.out.println(a.getNum()); //getting output as 5. Okay as Expected.
}
}
class C{
A a;
public static void main(String[] args){
B b = new B();
a = b.getInstanceOfA();
System.out.println(a.getNum());
//Here I'm getting output 0 not 5 why? As Thread created on instance a is
//already running, and also I am using the same instance of class A
//so I should get the updated value 5, but getting 0. Why it is re-initializing num?
}
}
You can also use Singleton
http://en.wikipedia.org/wiki/Singleton_pattern
I have something similar to:
public class A{
public static B[] bObj;
public A(){
bObj = new B[2]; // create array of objects of class B
}
public static void main(String[] args){
A aObj = new A();
for(i=0;i<2;i++){
bObj[i].testprint();
}
}
}
class B{
public testprint(){
System.out.println("Inside testprint()");
}
}
When I run this, I get NullPointer exception at bObj[i].testprint(). I did do new B() in the constructor of A. But I don't know why it isn't working.
Understand that initializing an array of reference, doesn't really initializes the references inside it. They are still null. You need to initialize them by iterating over the array.
public A(){
bObj = new B[2];
for (int i = 0; i < bObj.length; ++i) { bObj[i] = new B(); }
}
I want to create an object using a method but I want it (object+reference) to live even after the method ends. Example of code:
public class start{
public static void main (String [] Args){
public void createObject(){
object1 createdObject = new object1();
}
createObject();
createdObject.doSomething();
}
}
public class object1{
//code for object 1
}
So my main question is: how to create object using method and let it live even after method ends. The problem is that reference createdObject is popped of stack after method ends and therefore I can't use it anymore. Is it even possible to create object for further use this way?
public class start{
public static void main (String [] Args){
//class level scope
object1 createdObject = null;
private void createObject(){
createdObject = new object1();
}
}
public class object1{
//code for object 1
}
NOTE: I have not following naming conventions. But please follow them in actual code
UPDATE: Proper code check it out
public class Main {
public static void main(String[] args) {
new MyClass().doSomething();
}
}
class MyClass{
Object obj ;
public void doSomething(){
createObject();
}
private void createObject(){
obj = new Object();
System.out.println("Created MyClass instance");
}
}
Your method should return object1 instead of void and you have to add the following line at the end of the method:
return createdObject;