Page 1 of 1

Intro to Designing Classes Introduction for Classes with non-static methods Rate Topic: ***** 1 Votes

#1 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 29 July 2008 - 02:28 PM

Well, if you're reading this, you most likely do not have a lot of Java experience. No offense to anyone that reads this that DOES have a bit of experience, but this is just a basic concept.

Well, let's get started. :)

Now then, classes that have a need of objects...this is what we are going to make.

I'll start with an example that some people like, a Character (for a video game) class. Let's start with the basic header for our class and a constructor.

class Character
{
	public Character()
	{
	}
}


Right now, what's in that code is the class construct itself and the constructor. Notice how the constructor has the EXACT same name as the class. This is important. If it doesn't have the same name, it's not a constructor.

Well, this is not a very good class yet, since we don't have any data...or methods...or anything besides a constructor...so let's add some! A Character needs HP and MP, right?

class Character
{
	private int HP;
	private int MP;

	public Character()
	{
	}
}


We declare these global to the class itself, so that any entity inside the class can access them. Notice how we declare these private. Declaring them private makes it so that they can only be accessed and/or changed by members of it's own class (AKA the methods). If we had declared these public, they would be directly accessible outside of the class, and not just through the methods of the class.

Well, now we have data, but it's never initialized, let's add that real quick. :)

class Character
{
	private int HP;
	private int MP;

	public Character(int hp, int mp)
	{
		this.HP = hp;
		this.MP = mp;
	}
}


One change that might not be obvious here is that the parameters of the constructor changed. The parameters are what's inside the parentheses. These are passed to the constructor when we use it. It needs to know these values so that it can assign values accordingly.

Notice the this keyword. This is a pointer to the object that is calling the method. If we called it like this -- OBJECT.method();, in that case, the this keyword will reference OBJECT. It just represents the current object and all of its data.

Now we have some HP and MP. Hooray! But now we need some methods to access these variables, right? RIGHT, Locke!

class Character
{
	private int HP;
	private int MP;

	public Character(int hp, int mp)
	{
		this.HP = hp;
		this.MP = mp;
	}

	public int getHP()
	{
		return this.HP;
	}

	public int getMP()
	{
		return this.MP;
	}
}


Well, now we have some methods to access our attributes. Notice the return type before the method heading...it NEEDS to be there, so that the method knows what it's supposed to be returning. The return type is always the same as the variable type in basic return methods like these, that just access and tell you what the variable value is.

So if we have something like this...

private String name;

public String getName()
{
	return this.name;
}


Well, with our private declaration, we can't change our HP or MP values, only access them with our methods, so let's add some set methods.


class Character
{
	private int HP;
	private int MP;

	public Character(int hp, int mp)
	{
		this.HP = hp;
		this.MP = mp;
	}

	public int getHP()
	{
		return this.HP;
	}

	public int getMP()
	{
		return this.MP;
	}

	public void setHP(int newHP)
	{
		this.HP = newHP;
	}

	public void setMP(int newMP)
	{
		this.MP = newMP;
	}
}


We MUST have parameters in our set methods, or Java doesn't know what to set the values to. Notice the void keyword in the method heading...this means the method doesn't return anything. It doesn't need to, since it's just changing values.

SUM UP

To create a new object of our Character class:

Character locke = new Character(50, 50);
// locke is the variable name
// starts out with 50 HP and 50 MP


To retrieve values:

int lockesHP = locke.getHP();
int lockesMP = locke.getMP();


To set values:

// set locke's HP to 100
locke.setHP(100);

// set locke's MP to 55
locke.setMP(55);

______________________________________

Well, this is just a very basic tutorial in designing Object methods/classes. If you want to know how to make classes that don't require instances to function, you can look at my other tutorial here.

Bye! :D

Is This A Good Question/Topic? 4
  • +

Replies To: Intro to Designing Classes

#2 daelious  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-April 08

Posted 09 January 2009 - 07:27 AM

Thanks for this tutorial. Got me back up to speed with Java really quick. :)
Was This Post Helpful? 0
  • +
  • -

#3 dashonna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-September 06

Posted 23 April 2009 - 02:53 PM

Very well put together info here. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Nashkyle*


Reputation:

Posted 26 April 2009 - 03:57 AM

Thanks
Was This Post Helpful? 0

#5 XxdejavuxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-April 09

Posted 28 April 2009 - 08:58 AM

thank you, very clear explanation :D
Was This Post Helpful? 0
  • +
  • -

#6 lucianoefe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-October 08

Posted 12 May 2009 - 10:07 AM

hello there, this is rily nice of u to kip this here helping guys like us. got a question though, hw do i do this using netbeans ide 5.0?
Was This Post Helpful? 0
  • +
  • -

#7 becstribe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 09-April 09

Posted 21 May 2009 - 02:48 AM

Cheers thanks for that :)
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Posted 06 June 2009 - 07:20 PM

Well, it seems I've been neglecting this a bit.

Doing this in NetBeans is nearly the same as doing it anywhere else. Just create a new project, make a new file, and keep your class in there. As long as you include it in the same project package, you can use it anywhere else in the project. And if you want to use it in a different project, then you must create a .jar file and link the project to it, then import the package.

Hope this helps! And thanks for the thanks! :)
Was This Post Helpful? 0
  • +
  • -

#9 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Posted 13 October 2009 - 02:35 AM

:^: Dang that is a huge help to me since we are only starting to get this learnt in college. Yes, i'm learning java and i'm not as experienced as i should be but some time or later i might get the heng of things and get better. Sure that will take years and years in my situation but i will get there eventually :crazy: :P :crazy: :D
Was This Post Helpful? 0
  • +
  • -

#10 JavaT  Icon User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 70
  • Joined: 21-February 11

Posted 02 October 2011 - 09:08 AM

Thanks a lot. This really helped me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1