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: Java Static Method

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java Static Method

    Could someone give me a hints on how I can return the answer to "surfaceGravi" I get incompatibles type when I try to return it to surfaceGravi. Thanks

    public class GravityV1
    {
        // Calculate surface gravity
        public static double surfaceGrav( double []massKilo, double[] diameter)
        {   double gravConstant = 6.67E-11;
            double []surfaceGravi= new double[9];
            for(int count = 0; count < surfaceGravi.length;count++)
            {
            surfaceGravi[count] = ((gravConstant * massKilo[count]) / (Math.pow(diameter[count]/2,2)));
            }
            return surfaceGravi;
        }
     
        public static void main(String[]args)
        {
            String [] planetName = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
            double [] massKilo = {3.30E23,4.869E24,5.972E24,6.4219E23,1.900E27,5.68E26,8.683E25,1.0247E26,1.27E22};
            double [] diameter = {4880,12103.6,12756.3,6794,142984,120536,51118,49532,2274};
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Static Method

    get incompatibles type
    Please post the full text of the error message that shows the source statement where the error happens.
      for(int count = 0; count < 9;count++)
    You should the array's .length attribute to limit the looping, not a magic number:9
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java Static Method

    I fixed it on the post, thanks. ~Incompatibles type at return surfaceGravi;

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Static Method

    What data type is the method defined to return?
    What data type is surfaceGravi?
    If they are not the same or compatible, then you need to change one or the other:
    Either Define the method to return surfaceGravi's type
    Or Return a value of the correct type
    If you don't understand my answer, don't ignore it, ask a question.

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

    maple1100 (January 3rd, 2013)

  6. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java Static Method

    Oh, I didn't realize one is double and the other one is double array. Thanks you so much!

Similar Threads

  1. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  2. What's the difference between a static and non-static method?
    By wholegrain in forum Java Theory & Questions
    Replies: 4
    Last Post: February 23rd, 2012, 01:06 AM
  3. Replies: 1
    Last Post: February 14th, 2012, 07:42 AM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM