-1

I am currently working on a Triangle related object class, and I am having trouble with my static method.

For the homework assignment, my professor specifically wants this

A static method isTriangle() that accepts 3 double values as lengths of sides and returns true if the given sides can form a triangle, otherwise returns false. [help: sides s1, s2 and s3 can form a triangle IF s1 < s2 + s3 and s2 < s1 + s3 and s3 < s1 + s2].

What I currently have is:

package Homework;

import java.util.Scanner;

public class Triangle {
  Scanner scan1 = new Scanner(System.in);
  private double side1;
  private double side2;
  private double side3;

  public Triangle(double s1, double s2, double s3) {
    this.side1 = s1;
    this.side2 = s2;
    this.side3 = s3;
  }    

  public static boolean isTriangle(Triangle verify) {
    return (side1 < side2 + side3 && side2 < side1 + side3 && side3 < side1 + side2);
  }    
}

This is the error message I am getting:

Cannot make a static reference to the non-static field.
6
  • Actually I believe that you're looking for a shortcut asking this on StackOverflow. Just follow the assignment: your isTriangle has nothing to do with the proposed in the whole exercice. Commented Oct 14, 2018 at 9:37
  • If you want to access the fields of your Triangle object verify, then you should use verify.side1 etc. Commented Oct 14, 2018 at 9:43
  • @MatíasFidemraizer I'm not really asking for a shortcut or a blatant answer. This has more to do with why I'm getting an error message. Commented Oct 14, 2018 at 9:44
  • @Zepheriah Check my answer. I'm not going to provide you the solution since it's very obvious: read the exercice carefully! Commented Oct 14, 2018 at 9:49
  • @Roshana Don't give answer for the sake of points. Let him/her understand the concept first. Commented Oct 14, 2018 at 9:59

5 Answers 5

3

You can't access instance variable inside a static method. You can only access static variables or static methods directly inside a static method .So use the parameter in isTriangle() method.

Sign up to request clarification or add additional context in comments.

Comments

0

You can modify the static function to the following:

public static boolean isTriangle(Triangle verify) {
  return verify.getSide1() < (verify.getSide2() + verify.getSide3()) 
     && verify.getSide2() < (verify.getSide1() + verify.getSide3()) 
     && verify.getSide3() < (verify.getSide1() + verify.getSide2());
}

FYI: I have not tested the actual code. You should also use getter/setter or make the private fields to public to use verify.side1 instead of verify.getSide1().

Comments

0

You have to use parameter verify in your static method, instead of fields in Triangle class. Eg:

public static boolean isTriangle(Triangle verify) {
    return (verify.side1 < verify.side2 + verify.side3 && verify.side2 < verify.side1 + verify.side3 && verify.side3 < verify.side1 + verify.side2);
  }  

Comments

0

You're trying to access instance variables inside a static method. Hence the error "cannot make static reference to the non-static fields". I do not want to give away more clues as this is an assignment and you should figure it out by yourself.

Comments

0

You're trying to access instance members from a static one - this isn't ever possible -. This is the source of the error.

About the assignment, just follow its instructions:

A static method isTriangle() that accepts 3 double values as lengths of sides and returns true if the given sides can form a triangle <--

I don't see a static method with 3 inputs of type double. You'll get the answer yourself if you just do what your teacher has proposed you in the whole exercice.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.