Results 1 to 8 of 8
Thread: Classes inside of methods...?
- 01-16-2013, 08:48 PM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Classes inside of methods...?
Hey guys; I had a general question that I'm not quite getting. In methods, such as the paint method for swing, why are objects declared as parameters? Let me give an example.
Why is the Graphics object being declared in parameters? Plus, no instance of it is being created. How does this work?Java Code:public void paint(Graphics g) {}
- 01-16-2013, 08:58 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Classes inside of methods...?
You can pass Objects as parameters.
Java Code:class Person{ private String name; public Person(String name){ this.name = name; } public String getName(){ return name; } }Java Code:class Test{ public static void main(String[] args){ Person me = new Person("sleepin"); printName(me); } public static void printName(Person someGuy){ String name = someGuy.getName(); System.out.println("My name is " + name); } }
- 01-16-2013, 10:38 PM #3
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-16-2013, 10:42 PM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
- 01-16-2013, 10:51 PM #5
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
- 01-16-2013, 10:58 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Classes inside of methods...?
No, they also don't have to do
don't they?Java Code:public static void printName(Person someGuy = new Person()){
It is just method signature, you should read about painting components to better understand this.Last edited by milovan; 01-16-2013 at 11:05 PM.
- 01-17-2013, 12:44 AM #7
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Classes inside of methods...?
I think I got it. Thank you very much guys
-
Re: Classes inside of methods...?
A Graphics object is being created, but not by you. It's being created behind the scenes by the GUI library and then passed into this method when the GUI library calls it. That's one of the reasons why we do "passive" drawing, why we let the GUI library do the drawing itself.
Similar Threads
-
Why and where abstract methods & classes and static methods used?
By ajaysharma in forum New To JavaReplies: 1Last Post: 07-12-2012, 11:04 PM -
Why and where abstract methods & classes and static methods are used?
By ajaysharma in forum Advanced JavaReplies: 2Last Post: 07-12-2012, 11:04 PM -
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Use classes inside of other classes
By BlankFile in forum New To JavaReplies: 2Last Post: 02-06-2012, 01:51 PM -
Using variables inside methods?
By Forty0ztoFreedom in forum New To JavaReplies: 2Last Post: 04-08-2011, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks