Could some one tell me how can i get variable inside methods and the reverse.
Something like:
i want to use variable y inside that method func, and get that x from that method func and use it inside main.
class test{
int y = 4;
void func(){
int x = 3;
}
public static void main(String[] args)
{
// take x inside main
}}
You can always use class variable inside methods. To use x of func() inside main() method, you can return it from func() or save it into some class variable
class TestClass {
int y = 4;
int x = 0;
//func returning x
int func1() {
int x = y;
return x;
}
//func storing it to class variable
void func2() {
this.x = 3;
}
public static void main(String[] args) {
TestClass t = new TestClass();
int xOfFunc = t.func1();
t.func2();
System.out.println("x Of Func :: " + xOfFunc + "\n class variable x :: " + t.x);
}
}
output :
x Of Func :: 4
class variable x :: 3
class test{
int y = 4;
int func(){
int x = 3;
return x;
}
public static void main(String[] args)
{
test obj = new test();
int x = obj.func();
}
}
or you can make func() method static and you will be able to call this method without creating an object of class:
class test{
int y = 4;
static int func(){
int x = 3;
return x;
}
public static void main(String[] args)
{
int x = func();
}
}
Try something like this:
class Main {
public int y= 4;
int func(){
return 4;
}
public static void main(String... args){
Main m = new Main();
int x = m.func();
int y = m.y;
}
}
class test{
int y = 4;
int x;
void func(){
int x = 3;
this.x = 3; //make it usable from the class
}
}
y should be accessible inside the function. If the function uses a variable y by itself you can use this.y to access the variable.
Make it static like this allows you to access it everywhere by calling test.y.
class test{
public static int y = 4;
void func(){
int x = 3;
}
}
Then you can do this in main.
public static void main(String[] args)
{
int value = test.y;
}
Related
I have read that a static method can't call a no-static method, but this compile, the main(static) method call maybeNew(no-static) method, can you give me a clue?
public class Mix4 {
int counter = 0;
public static void main(String[] args) {
int count = 0;
Mix4[] m4a = new Mix4[20];
int x = 0;
while (x<9) {
m4a[x] = new Mix4();
m4a[x].counter = m4a[x].counter + 1;
count = count + 1;
count = count + m4a[x].maybeNew(x);
x = x + 1;
}
System.out.println(count + " " + m4a[1].counter);
}
public int maybeNew(int index) {
if (index<5) {
Mix4 m4 = new Mix4();
m4.counter = m4.counter + 1;
return 1;
}
return 0;
}
}
You can not call a non-static method directly from a static method but you can always call a non-static method from a static method using an object of the class.
public class Main {
public static void main(String[] args) {
// sayHello(); // Compilation error as you are calling the non-static method directly from a static method
Main main = new Main();
main.sayHello();// OK as you are calling the non-static method from a static method using the object of the class
}
void sayHello() {
System.out.println("Hello");
}
}
public class Store {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one*two*three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne,sideTwo,sideThree);
System.out.println(mult.area);
}
}
Can anyone help a beginner understand why, when passing parameters, this is an invalid method declaration?
You define/call a Calc constructor, but there is no Calc class.
Rename your class to Calc ant your code will compile and execute correctly:
public class Calc {
// instance fields
int area;
// constructor method
public Calc(int one, int two, int three) {
area = one * two * three;
}
// main method
public static void main(String[] args) {
int sideOne = 2;
int sideTwo = 3;
int sideThree = 1;
Calc mult = new Calc(sideOne, sideTwo, sideThree);
System.out.println(mult.area);
}
}
interface Interf {
void m1();
}
public class Test {
int x = 888;
Interf f;
public void m2() {
Integer x = 777;
f = new Interf() {
Integer x = 666;
public void m1() {
int x = 555;
System.out.println(x);
System.out.println(this.x);
}
};
}
public static void main(String[] args) {
Test t = new Test();
t.m2();
t.f.m1();
}
}
How can I access x variable with value 777 inside m1() method(In Anonymous class) with same name? Is it possible to access?
No, because it is shadowed. But you can change the name (and no need for Integer, int will suffice).
public void m2() {
int y = 777;
f = new Interf() {
int x = 666;
public void m1() {
int x = 555;
System.out.println(x);
System.out.println(this.x);
System.out.println(y);
}
};
}
Outputs
555
666
777
I have coded and run my model in OPL, and I am trying to run code it in JAVA and run it again. As part of my Code (in OPL), I have defined a Tuple as follows:
int Y = asSet(1..7);
int k = asSet(1..42);
int G = asSet(1..2);
tuple SL {
int i;
int j;
float l;
}
{SL} SLs with i,j in Y=...; /* e.g. {<1,2,502>, <2,5,309>, <5,7,401>, <2,3,350>} */
Then, I have defined other arrays of:
int u[SLs][G]=...; /* e.g. u[<1,2,502>][1] = 50; u[<1,2,502>][2] = 83; u[<2,5,309>][1] = 75;*/
Now that I wanted to code it in Java, I have done it as follows, but I am not sure if I am right. I would appreciate if you could share your ideas.
import java.io.*;
import java.util.*;
public class Model {
public static int Y = 7;
public static int K = 42;
public static int G = 3;
public static int R = 2;
public class SL {
public int i; /* how say i is in Y*/
public int j; /* how say j is in Y*/
public int l;
List<SL> sl = new ArrayList<SL>();
Object[] SL1 = Sl.toArray();
int [][] u = new int [sL.length][G];
}
public static void Solve() {
/* How to instantiate SL1 and u[<i,j,l> in SL1][g in G] here and printout SL1:*/
}
}
and another class to run the solve() method:
public class SolverMethod {
public static void main(String[] args) {
Model.Solve();
}
}
I would appreciate if you help me out to fix and run the code.
Regards,
Bornay
You may make Map so that with tuple or object of SL you can generate unique number which can be used as a index in u var. say,
Map<SL,Integer> m;
int key=m.get(sl);
u[key][g]
and to instatiate SL1 you need to make object of SL since SL1 is not static.
SL sl=new SL();
sl.SL1 or sl.u
First create object of SL and point it's variables or methods.
Here is my implemented code below. I have made some changes.
import java.io.*;
import java.util.*;
public class Model {
public static int Y = 7;
public static int K = 42;
public static int G = 3;
public static int R = 2;
static Map<SL,Integer> m=new HashMap<>();
static List<SL> sL = new ArrayList<SL>();
static int[][] u;
static int index=0;
static public class SL {
public int i;
/* how say i is in Y*/
public int j;
/* how say j is in Y*/
public int l;
}
public static void Solve() {
/* How to instantiate SL1 and u[<i,j,l> in SL1][g in G] here and printout SL1:*/
for(int i=0;i<5;i++){
SL sl=new SL();
sl.i=i;sl.j=i+1;sl.l=i+2;
sL.add(sl);
m.put(sl, index++);
}
u=new int[m.size()][G];
for(SL s:sL){
for(int i=0;i<G;i++){
u[m.get(s)][i]=i+10;
}
}
for(SL s:sL){
for(int i=0;i<G;i++){
System.out.println(u[m.get(s)][i]);
}
}
}
public static void main(String[] arg){
Model.Solve();
}
}
Here I have made sL,m and u static because we need only single instance of it.
My singleton class:
public class XandY {
private double x, y;
private static XandY xy;
//Constructor sets an x and y location
private XandY() {
x = 210.0;
y = 100.0;
}
public static XandY getXandY() {
if (xy == null)
xy = new XandY();
return xy;
}
public void updateXandY() {
x += 10;
y += 5;
}
}
Other class that changes singleton values and tries to reinitialize. My question is if I call changeXandY a few times then want to call resetXandY how do I make it reset back to the original x and y?
public class GameWorld {
private List<GameObject> objects;
public void initialize() {
objects = new ArrayList<GameObject>();
objects.add(XandY.getXandY());
...add other objects that are not singletons
}
public void changeXandY {
for (int i=0; i<gameObject.size(); i++) {
if (gameObject.get(i) instanceof XandY)
((XandY)gameObject.get(i)).updateXandY();
}
public void resetXandY {
initialize();
}
}
For this use case, you could simply store them as default values. Such as
private double x, y;
private static XandY xy;
private static final double default_x = 210.0;
private static final double default_y = 100.0;
That way when you reset, just:
public void resetXandY {
this.x = default_x;
this.y = default_y;
}
That being said, you may want to change your default constructor to look the same way.
If you can make the XandY reference protected, you can use a static initializer in an anonymous subclass:
// I need to reset the singleton!
new XandY(){
{ xy = null; }
};
But really, if you need to be able to (re)initialize the singleton, you should put a method to that effect into its signature. Obscure solutions are, at best, still obscure...
Create a resetXandY() method to set default value:
public class XandY {
private double x, y;
private static XandY xy;
//Constructor sets an x and y location
private XandY() {
x = 210.0;
y = 100.0;
}
//reset x=0 and y=0
public void resetXandY() {
x = 0;
y = 0;
}
public static XandY getXandY() {
if (xy == null)
xy = new XandY();
return xy;
}
public void updateXandY() {
x += 10;
y += 5;
}
}