7 Replies - 2205 Views - Last Post: 23 June 2009 - 07:03 PM Rate Topic: -----

#1 muirix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-June 09

Theory: Static methods vs Instance Methods

Post icon  Posted 16 June 2009 - 09:04 PM

Hey, thanks for the time.

So static methods and instance methods. What are the differences?

I think I know the answer, just not sure how to write it out clearly.

Static variables can be accessed through the class, so does static variable = class attributes?

Is an instance variable just a variable inside a method? (int x = null; ?)


So then, what's a static method? an instance method?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Theory: Static methods vs Instance Methods

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Theory: Static methods vs Instance Methods

Posted 16 June 2009 - 09:14 PM

OK lets add a unique ID to all your employees

class Employee {
   static int employeId = 1000;	  // share by everybody
   int id;									  // proper to each employe
   String name;						   // proper to each employee

   // constructor to add an employee
   Employee(String name) {
	  this.name = name;
	  id = employeId++;
   }
}



So the first Employe will have an id of 1000
then employeId, which is share by all instance of Employee will be incremented by one
next instance of Employee will have an id of 1001
... and so on
Was This Post Helpful? 1
  • +
  • -

#3 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Theory: Static methods vs Instance Methods

Posted 16 June 2009 - 09:16 PM

Every class has the potential to create objects of the class. Imagine a cookie cutter and cookies in the shape of the cookie cutter. The cookie cutter is the class and the shapes are objects of the class. Instance variables are the attributes of the objects of the class and instance methods are the operations that you can perform on objects of the class.

Class variables and methods are a part of the class that is shared by all of the class's objects. If you're class is a name, a class method might return how long the string of the name is. Hope that clears it up a bit.
Was This Post Helpful? 0
  • +
  • -

#4 muirix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-June 09

Re: Theory: Static methods vs Instance Methods

Posted 16 June 2009 - 09:24 PM

hey thanks for the help
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Theory: Static methods vs Instance Methods

Posted 16 June 2009 - 10:15 PM

View Postmuirix, on 16 Jun, 2009 - 11:04 PM, said:

So then, what's a static method? an instance method?


I asked this same question nearly a year ago, but it was in C#. I am not sure how closely Java and C# are related, but it seems to me that the concept should be similar for both languages if not exactly the same. (But I don't know Java, so sorry if it's not the same!)

http://www.dreaminco...wtopic56094.htm

This post has been edited by OliveOyl3471: 16 June 2009 - 10:21 PM

Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Theory: Static methods vs Instance Methods

Posted 17 June 2009 - 04:24 AM

View PostOliveOyl3471, on 16 Jun, 2009 - 09:15 PM, said:

View Postmuirix, on 16 Jun, 2009 - 11:04 PM, said:

So then, what's a static method? an instance method?


I asked this same question nearly a year ago, but it was in C#. I am not sure how closely Java and C# are related, but it seems to me that the concept should be similar for both languages if not exactly the same. (But I don't know Java, so sorry if it's not the same!)

http://www.dreaminco...wtopic56094.htm


exactly the same thing
Was This Post Helpful? 1
  • +
  • -

#7 muirix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 05-June 09

Re: Theory: Static methods vs Instance Methods

Posted 23 June 2009 - 05:56 PM

ah ok i'm seeing the picture more and more, thanks for the follow-up answers and confirmations!
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Theory: Static methods vs Instance Methods

Posted 23 June 2009 - 07:03 PM

No problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1