Results 1 to 8 of 8
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default 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.
    Java Code:
    public void paint(Graphics g) {}
    Why is the Graphics object being declared in parameters? Plus, no instance of it is being created. How does this work?

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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);
        }
    
    
    }

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Classes inside of methods...?

    Quote Originally Posted by Wnt2bsleepin View Post
    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);
        }
    
    
    }
    Ok, but they are not declaring the object of the type Graphics.

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Classes inside of methods...?

    Quote Originally Posted by MW130 View Post
    Ok, but they are not declaring the object of the type Graphics.
    The method would have to be called with a Graphics object.

  5. #5
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Classes inside of methods...?

    Quote Originally Posted by Wnt2bsleepin View Post
    The method would have to be called with a Graphics object.
    But don't they have to do Graphics g = new Graphics();?

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Classes inside of methods...?

    No, they also don't have to do
    Java Code:
    public static void printName(Person someGuy = new Person()){
    don't they?

    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.

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Classes inside of methods...?

    I think I got it. Thank you very much guys

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Use classes inside of other classes
    By BlankFile in forum New To Java
    Replies: 2
    Last Post: 02-06-2012, 01:51 PM
  5. Using variables inside methods?
    By Forty0ztoFreedom in forum New To Java
    Replies: 2
    Last Post: 04-08-2011, 07:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •