I know how to use layouts and everything, but my questions is, why do I have to do a new on the FlowLayout?
myPane.setLayout(new FlowLayout());
I really just need an explanation on why it isn't.
myPane.setLayout(FlowLayout());
Thanks in advance.
Printable View
I know how to use layouts and everything, but my questions is, why do I have to do a new on the FlowLayout?
myPane.setLayout(new FlowLayout());
I really just need an explanation on why it isn't.
myPane.setLayout(FlowLayout());
Thanks in advance.
You use new to create a new instance of a class. Using FlowLayout() without new looks like a call to a method not a call to make a new instance of a class.
Hey Norm, thanks for the response. Im still a little confused then. According to the code, setLayout is a method right? How does a method take a class an know what to do with it? I mean would the method setLayout look something like this?
public void setLayout(object layout)
{
layout newlayout = new layout();
(other instructions)
}
Now im not even sure that would work, but its the only way I can think of with my very very very very small experience with java, to access a class from a method inside another class, called from the another class.
That would be what the programmer that wrote the method would define and take care of. By class I assume you mean an instance of a class.Quote:
How does a method take a class and know what to do with it?
Quote:
would the method setLayout look something like this?
Code :public void setLayout(object layout)
That defines a method named setLayout that takes a parameter of type object named layout. Somewhere the class named object would have to be defined.
The next line is confusing because it has a classname the same as the parameter's name:
The syntax of that line says: Define a variable of type layout with a name of newlayout and assign it the value of a new layout object.Code :layout newlayout = new layout();
Alright thanks for the reply. I tried the scenario out that I suggested to no success. It said that object layout could not be found because the class did not exist. The only thing I could think of now is if the object passed into a method that handled every layout there was, without being lazy. Ive tried some more test and it seems that Ive gotten down using a new for classes, even when I'm not making a new object. The only thing I need to figure out now is how they did it. How they were able to get access to another class from a class instantiated from a class. Could figure it out if I could figure out how to compare to of the same classes.
You use new to create an instance of a class. What you just said does not make sense.Quote:
using a new for classes, even when I'm not making a new object.
I'm sorry if I didn't make sense. What I meant is I am use to doing stuff like
MyClass newClass = new MyClass();
not doing a new inside a parenthesis like this
newClass.getSomething(new Whatever());
I just can't figure out what to do with the (new Whatever()), or how to write a method in MyClass, that knows how to take another class(Whatever()) and actually tell
1: What class it is.
2:What to do with the class now that is knows what it is.
I guess Ill just copy and paste some of my code.
public class Test
{
public static void main(String[] args)
{
TestSubject subjective = new TestSubject();
subjective.setTestSubject(new Subject());
}
}
public class TestSubject
{
public void setTestSubject(Object newClass)
{
System.out.println(newClass.toString());
Object newSubject;
newSubject = new Subject();
System.out.println(newSubject.toString());
if (newClass.equals( new Subject()));
{
System.out.print("didnt work");
}
}
}
Have another class called subject that's just for test. It actually contains no code.
The first way assigns a reference to a variable that can be used elsewhere. The second way does not. They both create an instance of the class.
newClass.getSomething(new Whatever()); // Added needed ()s
Wow okay, I figured out I was doing everything right. I used a conditional statement to compare the classes, and if the classes were the same, it would print something out. I used print() instead of println() so I couldn't tell that is was actually working. Feel like an idiot now. Thanks for the help though.