first se these methods

setForeground(Color.cyan);
setBackground(Color.red);

these methods are defined in Component class like this

java.awt.Component

and we pass parameters of an other class this is very confusable for me that we pass class members as parameters to method of a different class, i mean setForeground(Color.cyan) i defined in Component class and this method is pass parameters of different class how it is possible plz plz plz plz plz plz give me key concept about this.i will be very thankfull to all members of this forum and management.

Recommended Answers

All 10 Replies

Are you asking why a method defined in Component can take a Color as its argument? Why wouldn't it be able to? It is perfectly common for methods to take objects of different in classes as arguments.

Even the standard main method takes an array of Strings as its argument even though it is not defined in the String class.

what is the relation between Color class and Component class. i mean a method of Component class is geting an Color class member as parameter i think any relation will be exist between them.

What's the relation between a class that has a main method and the String class? The relation is that there's a method that takes an instance of that class as an argument - that's all.

When the compiler is compiling the Component class, its source code will have an import java.awt.Color; or somesuch as part of its definition. That tells the compiler that Component class needs to use the Color class, and the compiler generates all the necessary code.

That's not really true. The compiler will know whether you're using java.awt.Color regardless of whether or not you use an import. All the import does is to allow you to write Color instead of spelling out java.awt.Color. It's just a convenience feature.

commented: Yes, Iknow. I was deliberately "over-simplifying" to try to get a point across to someone who was really struggling. JC +15

please write simple program that consist three classes and define a method in second class that take a member of first class as parameter and do somthing
ans third class has main mathod to call the methods.

class Run{

    static int x;
    //method if need...........
}
class RunDemo{

    void getParam(Run.x)
    {

        //Do Something..........
    }
}
class Main{

    public static void main(String s[])
    {

        //Methods Calling.......
    }
}

if you give me a simple program then my problem will be solve .

now I don't follow ....
what does an applet need a main method for? anyway, stop what you're doing, because you are heading in the wrong direction.

class RunDemo{
    void getParam(Run.x)
    {
        //Do Something..........
    }
}

this is not correct. either you know up front that it will always use the x value of the Run class, and you don't need to pass it as an argument:

class RunDemo{
    void getParam()
    {
       int x = Run.x;
        //Do Something..........
    }
}

and the call is just:

RunDemo run = new RunDemo();
run.getParam(); // which is a weird name for a method that doesn't return anything, btw

or, you need to pass it as an argument, at which time, you'll have something like this:

class RunDemo{
    void getParam(int param)
    {
        //Do Something..........
    }
}

and call it like this:

RunDemo run = new RunDemo();
run.getParam(Run.x);

in your current code, the compiler 'll have it hard figuring out what the exact parametertype is you are trying to pass. not to mention what might happen if you first compile this class, then change the type of Run.x to String and just re-compile that class.

brother i need a simple program that have three classes one method of any class get the object or any class member as its parameter and third class call it that's all ignore applets i need a simple program

class Location {

    double longitude, latitude;

    public Location(double longitude, double latitude) {
        this.longitude = longitude;
        this.latitude = latitude;
    }

    @Override
    public String toString() {
        return longitude + " " + latitude;
    }
}

class City {

    String name;
    Location location;

    public City(String name, Location location) {
        this.name = name;
        this.location = location;
    }

    public Location getLocation() {
        return location;
    }

}

class Tester {

    public static void main(String[] args) {
        City london = new City("London", new Location(48.3, 0.0));
        System.out.println(london.getLocation());
    }

}

brother i need a simple program that have three classes one method of any class get the object or any class member as its parameter and third class call it that's all ignore applets i need a simple program

well, in order to keep it clear what your thread is about, you should pick a title that is related to the subject.

anyway, if you need a simple program, why not write one? I showed you in my previous post how to change your code to get exactly that. but I do agree with James, that using instance rather than static variables is better.

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.