Jump to content

help with objects

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
say I have this code:

public class tuna {

	public void simpleMessage(String name){

		

	}

}


why don't I just do this:

public class tuna {

	public void simpleMessage(){

		String name;

		

	}

}


instead?

thanks for the help :D

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Because in the 2nd case name will always be what you coded it to be where in the first case you can give in another name every time you use the method.

#3
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
ooh yeah I get it. the first one will change "name" to whatever I input in my other class every time I call this method, right?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Ye, with the first one you can do
simpleMessage("Tom");
simpleMessage("Chris");
simpleMessage("Steve");

And in the second one you can only do
simpleMessage();
where it will always use the same name coded inside.

#5
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
ok perfect, thanks!

#6
AfroJack

AfroJack

    Newbie

  • Members
  • PipPip
  • 24 posts
For the 1st block of code you have you will need to put inside the construcotr something like

this.name = name;

Or something to store the variable, that wont save his name unless you store it




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users