Friday 15 May 2015

How can access a object in main class from another class in java? -



How can access a object in main class from another class in java? -

i have main class , 2 sub class in java language.how can access xx in class y?please help me ,i need in project.

import class x,y; public static void main(string[] args) { xx=new x; example=new y; } public class y{ alter xx.value;//how can alter it? }

if understand question, can accessor , mutator (or getter , setter if prefer) , like,

static class x { public x(int value) { this.value = value; } int value; public void setvalue(int value) { this.value = value; } public int getvalue() { homecoming value; } public string tostring() { homecoming integer.tostring(value); } } static class y { public void example(x x) { system.out.println("in example"); x.setvalue(x.getvalue() + 5); } } public static void main(string[] args) { x xx = new x(10); system.out.printf("xx = %s%n", xx); y yy = new y(); yy.example(xx); system.out.printf("xx = %s%n", xx); }

output is

xx = 10 in illustration xx = 15

java

No comments:

Post a Comment