0

I want to call a user defined function inside a function without creating an object or passing any value to the called function. Is there any possible way to do that? consider the following code

 import java.io.*;
 public class sorting
{
int number[]=new int[5];
void input()throws IOException
{
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter 5 numbers");
    for(int i=0;i<5;i++)
    {
        number[i]=Integer.parseInt(in.readLine());
    }
}

void sort()
{
    int no;
    for(int i=0;i<5-1;i++)
    {
        for(int j=i+1;j<5;j++)
        {
            if(number[j]<number[i])
            {
                no=number[i];
                number[i]=number[j];
                number[j]=no;
            }
        }
    }
}

void display()
{
    for(int i=0;i<5;i++)
    {
        System.out.print(number[i]+"\t");
    }
}

public static void main()throws IOException
{
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    sorting o1=new sorting();

    o1.input();
    o1.sort();
    o1.display();
}

}

this is a code for sequential sort. in void main i've created an object o1 and called the methods. but instead of calling 3 methods i want my job to be done by calling just one of them. by my knowledge that can be done by calling a function inside another function.

2
  • Think what you need is a static method Commented Jan 25, 2015 at 19:00
  • It is not clear what you are really looking for, but I guess you can use class function which does not need any object creation at all, but I do not know what you mean passing any value? do you have any code? Commented Jan 25, 2015 at 19:00

1 Answer 1

1

Yes, this is possible. The code below is an example that fulfills your conditions:

//static -> no object creation
public static void a() {
    b(); //call function inside a function
}

//static -> no object creation
//no arguments -> you do not pass any value to your function
public static void b() {
    System.out.println("output something");
}

This answers your question. If it does not help you, please rephrase your question so that it is more clear what your question is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.