Hey evyone,
I have three classes
I made objects out of classA in Class B...
For some refernce can i use those objects in class C...
Thanks
Printable View
Hey evyone,
I have three classes
I made objects out of classA in Class B...
For some refernce can i use those objects in class C...
Thanks
Yes, as long as C has a reference to them.
how do we refernce objects??
AnObject anObjReference = new AnObject();
anObjReference is a reference to an object.
i have object reference
ClassA[] obj=new classA[10]; in Class B
can i use 'obj[0]' in classC
How will class C get the reference?
Does class C have a reference to the class B object?
If it does, it can call a method in class B to get to the obj variable. For example:
have a method in Class B:
public ClassA getRef() {
return obj[0];
}
Then in class C, if there is a ref to class B: refClassB
ClassA objZero = refClassB.getRef(); // get obj[0] from class B
Norm ,
thanks
I have one issue
i have three classes
public class dd {
public static void main(String[] args) {
// TODO Auto-generated method stub
node[] nodeobj=new node[1];
nodeobj[0]=new node(2);
nodeobj[0].xy();
System.out.println(nodeobj[0].obj[0].y);
}
}
public class node {
int x;
public static di[] obj=new di[1];
obj[0]=new di(22);
public node(int p)
{
x=p;
}
public void xy()
{
}
}
public class di {
int y;
public di(int f)
{
y=f;
}
}
why can not i print nodeobj[0].obj[0].y
when i made a single object it printed nodeobj[0].obj.y
Please copy and paste here the output from when you execute the program.
Do you get errors? Copy and paste the full text here.
Please use code tags around your code when posting. See: BB Code List - Java Forums
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token ";", { expected after this token
Syntax error, insert "}" to complete MethodBody
Syntax error on token(s), misplaced construct(s)
Syntax error on token "void", @ expected
Syntax error, insert "}" to complete ClassBody
at node.<init>(node.java:7)
at dd.main(dd.java:11)
putting
public class node {
int x;
public static di[] obj=new di[1];
obj[0]=new di(22); ---------------------> from here
public node(int p)
{
x=p;
obj[0]=new di(22); -----------------> to here remove the error : why is this???????????????????????????????????????????
}
How are you compiling your program? The output you posted looks like an execution trace, not something from the javac command.
Your code must have some serious syntax errors. Look at the lines referred to in the error message.
NOTE: When you post your code, please put it in code tags.
See:BB Code List - Java Forums
The code you posted is very hard to read.
Code :
obj[0]=new di(22); ---------------------> from here
That line of code must be in a method.
Yeah You can call any functions or methods from any class. For that you have to make object of called class in calling class. By this you can pass any parameters to that class and also call functions or methods from that class. Or you can also make inheritance for that.