Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Calling upon methods

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Calling upon methods

    Hey guys, hows it goin
    im pretty new to java and programming, ive gotten better at writing methods individualy, however if i get a big project that uses a good few methods and within other methods i can never get my programme to compile. Basicly i need help on understanding how you call upon methods and what learning wtf "private" and "this" are. Id appreciate some advice on how to write the code for bigger programmes like the one that will be on my test which is a wordsearch.

    regards,
    J.Fox


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jonathanfox (August 3rd, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Calling upon methods

    think i know what im doing wrong now, when i use a method i create i never make a new object that uses the method, hopefully that made sense

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Calling upon methods

    That's seems backwards. When you want to call a method in a class from another method in another class, you need to have an instance of the class that you can used to call its method. If the code is executing in method aaaa() in class AA and wants to call method: bbb() in class BB:
    BB aBBInst = new BB(); // define an instance of BB somewhere in AA or in aaaa()
    ...
    in aaaa() that is in class AA:
    aBBInst.bbb(); // call method bbb() from method aaaa()
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Calling upon methods

    Quote Originally Posted by jonathanfox View Post
    ... wtf "private" and "this" are.
    PRIVATE:
    Private just means you can only read/write/use what ever it is you claim private from within the class from which it is declared. There are other options, see the link.

    THIS:
    This always refers back to itself.
    For example. You write a class called Apples. You write a class called BagOfApples.
    Within the BagOfApples class you have an ArrayList of the Apples which will belong to the BagOfApples class. ie the apples in the bag. The Apple class:
    /** File: Apple.java
     * @author jps
     *  This class represents an apple...
     */
    public class Apple {
     
    	public double diameterInInches;
    	public boolean hasWorm;
    	private boolean greatTaste;
     
     
    	/** Constructs an Apple object:
    	 * @param diameterInInches The diameter of the apple measured in inches.
    	 * @param hasWorm True if the apple has a worm in it, false otherwise.
    	 */
    	public Apple(double diameterInInches, boolean hasWorm) {
    		this.diameterInInches = diameterInInches;
    		this.hasWorm = hasWorm;
    		this.greatTaste = true;//of course my perfect apples have great taste!
     
    	}
     
            //Methods for the public to rate my apples after eating them, and review others feedback on them
     
    	/** @return True if the apple has a great taste, false otherwise. */
    	public boolean hasGreatTaste() {
    		return greatTaste;
    	}
     
    	/** @param greatTaste Set the great taste of the apple. */
    	public void setGreatTaste(boolean greatTaste) {
    		//this.greatTaste = greatTaste;
    		this.greatTaste = true;//I am a crooked salesman :P
    	}
     
    }

    Notice I used public on diameterInInches and hasWorm. I used public so that anyone anywhere can see the size of my apples, and that they have no worms. Public is a good modifier for things you want anyone anywhere to be able to get their hands on.
    Notice I used private on greatTaste. This means this variable can only be used from within the Apple class. The hasGreatTaste and setGreatTaste methods provide a way for anyone anywhere to get and set the variable's value. However, I am a crooked salesman. Hey it happens... My apples are so perfect, even if you try to set greatTaste to false, since the variable is private, and the only other access to it is through my crooked method, I can be sure every apple will always have greatTaste.

    So lets look at the first line inside the constructor:
    this.diameterInInches = diameterInInches;
    Where it says this.diameterInInches will refer to the instance variable of the specific apple object in question at the time. What that means is, assume we are tooling along in the BagOfApples class, filling our bag of appels. As we create each apple, we get a new set of variables that relate to the apple we are creating and putting in the bag. At that point the keyword this just refers to that apple.

    Now look at the parameters for the constructor. Notice how they have the same variable names as the variable names used as instance variables of the class. If you just type diameterInInches, the compiler looks within the current scope (at this point the constructor block) and finds the first variable named diameterInInches. The first one found will be the parameter, because it exists within the scope. The instance variable is hidden because once the parameter was found, the compiler assumes that is what you wanted to use and stops looking. By saying this.diameterInInches, the compiler knows you are trying to use the instance variable. Or the diameterInInches of this apple, and not the parameter with the same name.
    Quote Originally Posted by jonathanfox View Post
    ... some advice on how to write the code for bigger programmes...
    Ever heard the phrase "Practice practice practice"? Well in programming its another "p-word"... Pseudocode pseudocode pseudocode...
    It is like taking a trip. What do you have to do before you start driving? ...Figure out where to go! You get a map, you figure out your starting point and end point. Then you decide how to get there one road at a time. Do the same thing for your program. Start with a problem to solve. Decide what makes a solution acceptable. Then build your algorithm to get from start to end. Once you have that done you can translate your pseudocode to code. But until you have your pseudocode, trying to write code is like driving without directions.

    Ok so if you are going to drive to town, or make a simple apple class, you may not need directions or pseudocode. But if you have never been to town, or wrote a small class, it may be easier to get the hang of using your map/pseudocode on a small project, and not have troubles with that once the project is huge.

Similar Threads

  1. Calling methods in other files
    By zdavos in forum Object Oriented Programming
    Replies: 4
    Last Post: March 2nd, 2012, 07:57 AM
  2. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  3. Calling worker methods in JSF.
    By newbie in forum Web Frameworks
    Replies: 0
    Last Post: March 18th, 2011, 02:21 PM
  4. [SOLVED] Handling Errors / calling Error-Catching Methods
    By movsesinator in forum Object Oriented Programming
    Replies: 4
    Last Post: April 6th, 2010, 05:35 AM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM