Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

method call problems

I have two classes

class one contains
-Array with content
-Method to print the array

class two contains
-methods to manipulate the array in class 1
-calls the one.print

The problem is:
To print the manipulated array made by the methods in class 2, by calling the print method in class one.
I just get the default content when doing that.
0
m-jansen
Asked:
m-jansen
  • 3
  • 3
  • 3
1 Solution
 
aozarovCommented:
Are you sure you are manipulating directly the array that class one holds?
0
 
m-jansenAuthor Commented:
The array updated with the new content.
0
 
m-jansenAuthor Commented:
I think the problem is that I make a new instance when calling the print method
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aozarovCommented:
If you create a new array then make sure you are assiging the new array to class one array variable.
I hope the following is clear to you that it is a mistake and will not change the class one array:

in class two:
method XXX
{
array x = class one.array;
x = new array;
}
0
 
mightyoneCommented:

Your code should look somehow like this....

class A{
public String [] str = new String {"empty","full","full"};

public print(int i){
if (i > str.length){i = 0;}
System.out.println(i+"  :  "+str[i]);
}
}

class B{

private A classA = new A();
public B(){}
set(String s, int idx){
A[idx] = s;
}

public static void main(String args[]){
B classB = new B();
b.set("full",0);
b.print(0);
}}


}



if not change your array to static and it will work, but then you really have to make sure ONLY one Thrrad is manipulating your array.....
0
 
aozarovCommented:
I guess mightyone ment to write:

classA.str[iidx] = s;

instead of
>> A[idx] = s;


0
 
mightyoneCommented:
of course sorry for that ;-)
0
 
mightyoneCommented:
and change
set(String s, int idx){
A[idx] = s;
}
to

public void set(String s, int idx){
A[idx] = s;
}
0
 
m-jansenAuthor Commented:
Thanx
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now