Why I have this error in java [duplicate] - java

This question already has answers here:
What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?
(11 answers)
Closed 4 years ago.
I am a new learner in java, and I cannot figure out this error. I've already created a class outside the Main class, why can't I creat a object in Fraction?

Fraction is defined as inner non-static class inside Main class. Hence to instantiate an object of Fraction you would first need to define an object of Main. And using that define an object of Fraction.
e.g.
Main m = new Main();
Fraction a = m.new Fraction(in.nextInt(), in.nextInt());
Or the other option you have is to define Fraction class as static.
e.g.
static class Fraction {
... and you class definition...
}
The right option depends on your usage entirely. But as per the sample you pasted option #2 defining the class as static would suit you more.

Related

What do the instances of "this" mean in this code? [duplicate]

This question already has answers here:
What is the meaning of "this" in Java?
(22 answers)
Closed 4 years ago.
public class MyResults extends Results {
...public MyResults() {
this(5);
}
public double average() {
return this.getSum()/numberOfCourses;
}
}
What do both instances of ―this mean in the code?
First instance is a call to another constructor in the same class. This is also known as Constructor Chaining pattern. Since you didn't post the entire code we don't know if that other constructor is defined (it should be, otherwise you'll have a compile time error).
Second instance is a call to the getSum() method. This method might be defined either in MyResults class or Results class (or some parent class of Results, if any).

How do I get around my getter wanting a static method? [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 4 years ago.
My getter wants the method it's getting to have a static method but if I do that it will break my code is there any way around this?. Language Java.
This is the line in my test class that wants to get the method in my vending class.
System.out.println("Total Sales for both machines today $"+ VendingMachine.getTotalsSales());
This is my getter method that takes the array I created from the string provided in the test class that put the items into an array. I want to be able to keep this array that I have created while also getting the total sales.
public double getTotalsSales() {
totalSales += availableItems[this.selectItem].price;
return totalSales;
}
Is there any way I can keep my array and with also being able to grab the total sales from the tester class?
It sounds like VendingMachine is the name of your class, not the name of an instance of your class. Calling the method getTotalsSales on the class instead of an instance is trying to call it in a static context, which would be why it asks for it to be static.
Create an instance of the class and call it on that instead!
I assume that VendingMachine is the class name. You need to first create an instance of this class:
VendingMachine machine = new VendingMachine();
Then call the method on that instance:
machine.getTotalsSales()
For more details, I suggest you read a tutorial on the difference between classes and objects and how they are related.

How these two inner class objects differ? [duplicate]

This question already has answers here:
Does an inner class work as a composition relationship in java? [closed]
(3 answers)
Closed 5 years ago.
I'm trying to delevope intuition and familiarity with inner classes.
I'm apporaching myslef to this java's topic and after some research I've started practicing.
I came up with this piece of code that behaves in a way that I can't understand.
I created a class Example with a public inner class Inner.
Inside the Example class I created an object of the Inner class obj, and in the class MainClass, inside the main function I created another Inner class instance ( After creating the instance of the enclosing class).
Example class code:
public class Example {
Inner obj = new Inner(9);
class Inner {
public int x;
public Inner(int a) {
x=a;
}
}
}
Main class:
public class MainClass{
public static void main ( String args[] ) {
Example example = new Example();
System.out.println(example.obj.x); // output 9
Example.Inner obj = example.new Inner(10);
System.out.println(obj.x); //output 10
System.out.println(example.obj.x); // output 9
}
}
Given that the statements Inner obj= new Inner() and the statement Example.Inner obj = example.new Inner(10) both declare an object of the Inner class with the same name, I don't understand how this not give any problem.
I think it's reltaed ot how they are loded in memory, but they seem to be 2 completley different things but I don't understand how.
UPDATE:
Answers have been great till now, I edited the question changing the code and reducing the number of constructors to one. This should clarify what my doubts concern.
You have two instances of Inner class, although both instances have same name obj, they are separate from each other. Inner class instance defined outside main method is created using default constructor. So example.obj.x outputs 6.
Inner class instance obj defined inside main method is created using parameterized constructor so obj.x outputs 10.
Important thing here is that when you do example.obj.x, you are referring to the instance of Inner class created outside main method and when you do obj.x, you are referring to the Inner class instance created inside main method. These two instances of Inner are completely separate from each other.
EDIT
Answers have been great till now, I edited the question changing the
code and reducing the number of constructors to one. This should
clarify what my doubts concern.
Having one or two constructors won't change anything. Inner class instance defined outside main method and the one defined inside main method are two separate instances of Inner class.
x, data member of instance of Inner defined outside main has value of 9 and can only be accessed using Example class's instance example. Hence example.obj.x outputs 9.
Instance of Inner defined inside main is directly accessible using obj defined inside main and its x has a value of 10. Hence obj.x outputs 10.
You've created two objects: example, an instance of Example, and obj, an instance of Example.Inner. These two objects have nothing to do with each other, and share no information, so the x field in each can hold a different int.
Irrespective of the fact that you have an inner class, you have two completely different instances of Inner.
example.obj.x is assigned the value of 6 due to the constructor for Inner assigning 6 to x.
obj is instantiating a new instance (hence the keyword new), and then it's passing in the value 10 for its constructor.
This is why you get two different values; because these two instances are completely separate from one another and have no shared values between each other (i.e. static field), the values will be different.

Can an object belong to two classes? [duplicate]

This question already has answers here:
Does the System.out object belong to class System or class PrintStream? [closed]
(7 answers)
Closed 6 years ago.
Just started learning Java, through the "hello world" app, k learned about the object System.out.
Out is an object, but to use it, we have to write system in front of it. It's obvious and my book says that out belongs to system class.
But later in the book, my book says out also belongs to PrintStream class. That is what enable us to use println methods because they both belong to PrintStream class.
I am confused what class does out belong to?
Also, is it just a convention that for objects like out, we have to write the class as well whenever we use it. For something like;
String greeting = "Hello, World!";
If we want to use .length() method which I guess also belongs to string class, we DONT write:
int n=String.greeting.length()
Instead, it's just:
int n=greeting.();
out is a (static) member variable of class System.
It is an instance of class PrintStream.
class Foo {
String x;
}
x is a string. It is a member of class Foo. Same idea.

What exactly is MyClassName.class in Java [duplicate]

This question already has answers here:
What does .class mean in Java?
(8 answers)
Closed 8 years ago.
What exactly is the meaning of the following construction: MyClassName.class ?
At first I thought MyClassName.class would represent the access of a static class variable defined for MyClassName class, but it that'd be true then the following assignment should be possible:
MyClassName m = new MyClassName();
Class<MyClassName> clazz = m.class; //access static class variable by using an instance variable
What is the true meaning of MyClassName.class? Is it a static class variable? is it a Java special construction?
Every class in Java has an associated instance of java.lang.Class which contains metadata about the class i.e its attributes, types, methods, super class etc.
Here MyClassName.class is called class literal. Link to Java doc for further info - http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.8.2

Categories