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 4 of 4

Thread: Can someone please help with code evaluation and improvement

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone please help with code evaluation and improvement

    This code works, but I don't know if this is the best way to do this.
    I'm looking for help
    1) is it right
    2) is there a better way
    3) is there a more secure way

    /*
     * James Barber
     * 12/22/2013
     * passing values
     */
    package passing;
     
    public class PassingMethods {
     
    	static int passed1;
    	static int passed2;
    	static int passed3;
     
    	public static void main(String[] args) {
     
    		int passing = 10;
    		// printing passing value and the new value
     
    		System.out.println(passing);
    		passing1(passing);
     
    		System.out.println("\n" + passed1);
    		passing2(passed1);
     
    		System.out.println("\n" + passed2);
    		passing3(passed2);
     
    	}
     
    	// Receiving value from main, and then passing new value
    	public static void passing1(int passing) {
     
    		int passed1 = passing + 2;
    		System.out.println(passed1);
     
    		PassingMethods.passed1 = passed1;
    	}
     
    	// Receiving new value from 1, and then passing new value
    	public static void passing2(int passed1) {
     
    		int passed2 = passed1 + 2;
    		System.out.println(passed2);
     
    		PassingMethods.passed2 = passed2;
     
    	}
     
    	// Receiving new value from 2, and then passing new value
    	public static void passing3(int passed2) {
     
    		int passed3 = passed2 + 2;
    		System.out.println(passed3);
     
    		PassingMethods.passed3 = passed3;
     
    	}
     
    }// end of class


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone please help with code evaluation and improvement

    Welcome to the forum, good first post, but it would be helpful to know:

    What is the program supposed to be doing, and what do you mean by "more secure?"

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please help with code evaluation and improvement

    what I 'm trying to do here learn variable passing from method to method
    it's taking from main passing it,

    to first method then adding to it and passing the results
    then method 2 is taking the results and adding to it and passing those results
    then method 3 is taking the results and adding to it and passing those results
    the main is just printing the changes to the screen so I can see it's working

    goes as 10 comes out as 12
    goes as 12 comes out as 14
    goes as 14 comes out as 16

    more secure is dealing private verse public

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone please help with code evaluation and improvement

    And what are these statements supposed to be doing:

    PassingMethods.passed1 = passed1;

    The fact that your entire program is 'static' is making the demonstration you've described more difficult, next to impossible. From the main() method call a constructor that calls non-static methods using non-static variables. Return the results from the methods, and assign the results of the methods to variables or print them directly. That will be a much more useful demonstration of passing variables to and receiving/using the results of methods.

Similar Threads

  1. Replies: 1
    Last Post: November 16th, 2012, 03:31 PM
  2. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  3. [SOLVED] EVALUATION OF POSTFIX NOTATION
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 7th, 2011, 05:24 PM
  4. Evaluation of operators in expressions.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: September 23rd, 2011, 07:23 PM
  5. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 07:35 AM

Tags for this Thread