1

I get this error "no enclosing instance of type oppgave1 is accessible", when i wrote "new Triangle();". I need som help to explain me where i did wrong (about the triangle class). I'm new beginning in java.Thanks for advance.

public static void main(String[] args) {

    Triangle T1 = new Triangle(1, 1, 1, "green", false);

    Scanner input = new Scanner(System.in);
    System.out.println("Enter three sides of the triangle: ");
    double side1 = input.nextInt();
    double side2 = input.nextInt();
    double side3 = input.nextInt();
    System.out.println("Enter a color: ");
    String color = input.nextLine();
    System.out.println("Enter true or false (to indicate triangle is filled or no): ");
    String isFilled = input.nextLine();
}

public class GeometricObject {
    //Data fields
    private String color = "blue";
    private boolean filled;

    //The default geometricObject/constructor
    public GeometricObject() {
        this("No color", false);
    }
    //The geometricObject with the specified colour and filled value
    public GeometricObject(String color, boolean filled) {
        this.color = color;
        this.filled = filled;
    }
    //Returning the colour
    public String getColor() {
        return color;
    }
    //Setting a new colour
    public void setColor(String color) {
        this.color = color;
    }
    //Returning the filled
    public boolean isFilled() {
        return true;
    }
    //Setting a new filled
    public void setFilled(boolean filled) {
        this.filled = filled;
    }
    public String toString() {
        return (color + " - " + filled + " - ");

    }
}

public class Triangle extends GeometricObject {

    //Data fields
    double side1 = 1.0;
    double side2 = 1.0;
    double side3 = 1.0;
    //no-arg constructor
    Triangle(){
        this(0.0, 0.0, 0.0, "No color", false);
    }
    //A constructor that creates a triangle with the specified sides
    public  Triangle(double side1, double side2, double side3, String color, boolean filled) {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
        setColor(color);
        setFilled(filled);
    }
    //Returning the sides 
    public double getside1() {
        return side1;
    }
    public double getside2() {
        return side1;
    }
    public double getside3() {
        return side1;
    }
    //setting the new ones
    public void setSide1(double side1) {
        this.side1 = side1;
    }
    public void setSide2(double side2) {
        this.side2 = side2;
    }
    public void setSide3(double side3) {
        this.side3 = side3;
    }
    //getting the rule
    /* public void setSide(double side1, double side2, double side3) {
        if (((this.side1 +  this.side2) >  this.side3 ) && ((this.side2 + this.side3) > this.side1)
                && ((this.side1 + this.side3) > this.side2))
                System .out.println("The rule (the sum of any two sides is greather"
                        + " than the other side) is adhered"); 

    } */
    //Returning area
    public double getArea() {
        double p = ((side1 + side2 + side3) / 2);
        double area = Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
        return area;
    }
    //Returning perimeter
    public double getPerimeter() {
        return (side1 + side2 + side3);
    }

    public String toString() {
        return (super.toString() + "Triangle: side1 = " + side1 + "side2 = " + side2 + " side3 = " + side3 +'\n'+
                "Area i: " + getArea() + '\n' + "Perimeter is: "  + getPerimeter()) ;
    }
 }

}

aalku
  • 2,860
  • 2
  • 23
  • 44
Kurt16
  • 35
  • 2
  • 10
  • Your Triangle constructor has default visibility, maybe your main method lives in another package? Make it public and you should be fine. – Silviu Burcea Nov 08 '13 at 10:11
  • 1
    Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – Raedwald Mar 02 '16 at 22:40

1 Answers1

5

Triangle is currently an inner class - which means you can only create it by also having an instance of the enclosing class. Simple options:

  • Make Triangle a top-level (non-nested) class.
  • Make it a static nested class
  • Provide an instance of the enclosing class when constructing an instance of Triangle

(See the Java tutorial for more on nested/inner classes.)

Personally I'd recommend the first course of action - nested classes can certainly be useful, but I'd recommend using top-level classes unless there's a specific benefit from a nested class.

Additionally, even though you can put multiple classes in the same source file if at most one of them is public (and therefore has the same name as the file) I'd recommend putting each top-level class in its own file, named according to the class.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • In this specific case it seems like Triangle should be its own class with its own file. – odedsh Nov 08 '13 at 10:13
  • 2
    @odedsh: Indeed - a top-level class. – Jon Skeet Nov 08 '13 at 10:14
  • what do you mean by indeed a top level class? i'm ny beginner javas user and i want to understand what i'm doing. Thx – Kurt16 Nov 09 '13 at 11:14
  • @Kurt16: Did you follow the link in the answer, or search for "top level class" and Java? It's important to be able to do some of the research for yourself. – Jon Skeet Nov 09 '13 at 11:40
  • To be honest..not yet(saturday: spending day with my family). But I'll do it seriously, otherwise thank you for your answer & advice. – Kurt16 Nov 09 '13 at 13:21
  • Thanks. I understand the nested/inner classes now. I moved Triangle out of the Oppgave1 (norwegian, means excercise1)class. But, the code refuses to compile and i can't find where the pbm is. Can you help me with that (if you can tell me which line nr. the pbm is?) – Kurt16 Nov 11 '13 at 21:48
  • @Kurt16: How am I meant to know which line number the problem is on? Your compiler will tell you that. I suggest you do what you can to find the problem, and then ask a new question with all the relevant information - including the error message and which line it occurs on. – Jon Skeet Nov 11 '13 at 22:20
  • I found where the pbm was and i'm trying to resolve the remainder. Thanks for your help. – Kurt16 Nov 12 '13 at 09:52