Results 1 to 3 of 3
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Does a method location matters?

    Hi Mentors,

    I was going through parametrized constructors, and i am facing an issue with the location of the method definition.

    Please have a look at the code as below:-

    Java Code:
    class Box
    {
    	double width;
    	double height;
    	double depth;
    
    // Defining the parametrized constructors
    
     Box(double w, double h, double d)
    	{
    		if(w > 0)
    		{
    		width = w;
    		}
    		else
    		{
    			width = 1.0;
    		}
    		if(h > 0)
    		{
    			height=h;
    		}
    		else
    		{
    			height = 1.0;
    		}
    		if(d > 0)
    		{
    			depth = d;
    		}
    		else
    			depth = 1.0;
    
    
    	//Calculates and return the volume to the calling routines
    	
    	double getVol()
    	{
    		return depth*height*width;
    	}
    
    
    
    	}
    }
    
    class BoxDemo7
    {
      public static void main(String[] args)
       {
    	Box mbox1 = new Box(3,4,5);
    	Box mbox2 = new Box(2,3,4);
    
     System.out.println("Volume for 1st Box is:=" + mbox1.getVol());
    
     System.out.println("Volume for 2nd Box is:=" + mbox2.getVol());
    	}
    }

    The error the javac compiler giving me is as below:-
    Does a method location matters?-j-c-er.png


    But when i change the location of the getVol() function i.e above my parametrized constructor the code works fine for me.

    Please suggest the needful.

    Thanks in advance

    Ankit Gupta
    Last edited by ankiit; 12-28-2011 at 05:52 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Does a method location matters?

    Double check the bracket locations in your code....as posted, the getVol method is within the Box constructor

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Does a method location matters?

    Hi doWhile,

    Thanks a lot for the information, have made the necessary changes i.e moved the method out of the constructor body, now the code works fine.

    Thanks.
    Ankit

Similar Threads

  1. hiding passed parameters using window.location.href = 'params' method
    By kulangotski in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-01-2011, 01:49 PM
  2. JList Location
    By BeeGee in forum Advanced Java
    Replies: 8
    Last Post: 04-07-2010, 04:09 PM
  3. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 04:53 PM
  4. location of click
    By saima in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2009, 01:06 PM
  5. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •