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.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
If you don't understand my answer, don't ignore it, ask a question.
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.How does a method take a class and know what to do with it?
would the method setLayout look something like this?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.layout newlayout = new layout();
If you don't understand my answer, don't ignore it, ask a question.
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.using a new for classes, even when I'm not making a new object.
If you don't understand my answer, don't ignore it, ask a question.
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.
Last edited by johnvasgird; April 26th, 2012 at 03:21 PM.
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
If you don't understand my answer, don't ignore it, ask a question.
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.