hello everyone can u help me solve my problem...

i want to call a class in another class but i dont know how..example.

public class Example{
	public static void main(String[] args){
		int x;
		
		for(x = 0; x< 10; x++){
			System.out.print(x + " ");
		}
		System.out.println();
	}
}

and i want it to call in another class like:

public class Example1{
	public static void main(String[] args){
	
                       //some codes to insert to call it	

	}
}

to display the output of: 0 1 2 3 4 5 6 7 8 9

can u help me solve it..

Recommended Answers

All 7 Replies

You cannot call a class from within another class, but what you can do is call a public method of one class from another class. Here's an exmaple:

public class Demo{
    public void callMe(){
         // callMe code
    }
}

Now we will call the callMe method in another class. For that we do need an instance of the class Demo (which contains the callMe method) in the second class. Here's what we do:

public class Caller{
    public static void main(String [] args){
        Demo d = new Demo(); // create an instance of Demo called [I]d[/I].
        d.callMe(); // use d to call callMe
    }
}
public class Example {
  private double[] data;

  private int len;

  public Example(int max) {
    data = new double[max];
    len = 0;
  }

  public void insert(long value){
    data[len] = value; 
    len++;
  }

  public void display() {
    System.out.print("A=");
    for (int j = 0; j < len; j++)
      System.out.print(data[j] + " ");
    System.out.println("");
  }

  public void Example() {
    int inner, outer;
    double temp;
    
    int h = 1;
    while (h <= len / 3)
      h = h * 3 + 1; 

    while (h > 0) 
    {

      for (outer = h; outer < len; outer++) {
        temp = data[outer];
        inner = outer;

        while (inner > h - 1 && data[inner - h] >= temp) {
          data[inner] = data[inner - h];
          inner -= h;
        }
        data[inner] = temp;
      }
      h = (h - 1) / 3; 
    }
  }

  public static void main(String[] args) {
    int maxSize = 16;
    Example arr = new Example(maxSize);

    for (int j = 0; j < maxSize; j++) {
      long n = (int) (java.lang.Math.random() * 99);
      arr.insert(n);
    }
    arr.display();
    arr.Example();
    arr.display();
  }


}

what if i have this kind of code...how can i debug it to provoke or call it to another class?

The answer to your original question has been mentioned above. The question in your above post doesn't make any sense.

The answer to your original question has been mentioned above. The question in your above post doesn't make any sense.

the probLem is i cannot run it properly by calling it to another class.

still doesn't make any sense

look if you have another class like follows so

public class CallExample 
{
    //first create an instance of Example Class

}

look if you have another class like follows so

public class CallExample 
{
           public static void main(String args[])
    { 
      //first create an instance of Example Class
      Example obj = new Example();
     //then you can call public method using this object
      long ex =     obj.insert(1500);
      System.out.println(ex);
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.