Can an object belong to two classes? [duplicate] - java

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.

Related

why getClass() from superclass variable returns subclass class name? [duplicate]

This question already has answers here:
Dynamic dispatch and binding
(2 answers)
Closed 2 years ago.
Im confused, when i use getClass( ) from a superclass reference variable that's pointing to a subclass object, the result is the subclass.
Heres a simple example:
public `class` TestGetClass
{
public static void main(String[] args)
{
Object obj = new Integer(20);
System.out.println("obj class: " + obj.getClass());
}
}
The output gives me the Integer class instead of the Object class.
obj class: class java.lang.Integer
Can someone explain please
What you're looking for is simply:
Object.class.
obj.getClass() in java could plausibly be interpreted in two different ways:
It means: Take the expression 'obj', which is a reference (i.e., a pointer). Follow the pointer and find the object it is pointing at. Ask that object what its type is.
just like 1, except, because the variable type was Object, invoke the implementation of the getClass() method from the java.lang.Object class. i.e., no dynamic dispatch.
It means: Take the locally declared variable named obj. What type did I declare it as, right here in this method? Don't care about the object/pointer at all, just the declaration.
Now, the java lang spec is crystal clear: In java, #1 is what happens. #2 is not available (you can't opt out of dynamic dispatch. As a matter of obvious language design, private methods don't do it because they don't need it, and static methods don't do it because, by being static, they just aren't a part of the hierarchy in the first place - so those seeming exceptions really don't apply. There is no other way to opt out).
Here's the thing about option #2 though: is completely pointless.
In java, you can't have mystery meat variables. Somebody declares them, and the type is written right there in the source file. There is no such thing as 'eh, figure it all out at runtime'. Even java10's var doesn't work that way (it's still locked in, for sure, at compile time).
So, you already know. It is object, what point is there to repeat it?
If you want a java.lang.Class<?> instance that represents Object, there's syntax for this. it is:
Class<?> objClass = Object.class;

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 do open a file in another class than the one which gets the pathname [duplicate]

This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 7 years ago.
I built a GUI class, and I get the pathname of the file I want to open in this GUI class. The pathname comes back as:
public String filePath = "C\blablah";
I want to actually open my file in another class, the "Network" class, so I wrote the following code to get the path string in the Network class:
String readFile = GUI.file();
Path file = Paths.get(readFile, "Network");
I tried a few different ways but it doesn't work, I thought this one would work but it comes back with
"Cannot make a static reference to the non-static method filePath()
from the type GUI"
None of those classes are my main so I cannot instantiate a GUI in the "Network" class.
Please forgive if this is a newbie question.
Edit:
This is the method I wrote in the GUI class to access the filepath in other classes
public String file(){
return filePath;
}
Ok, what you trying to do here is to access the static variable "filePath" from your GUI class, but this variable isn't static. You can change this by adding the keyword static in front of the variable name.
But I guess, that you in fact don't want to access a static variable from the class GUI, but the variable of a GUI object. In this case there's no way around but to make a GUI object with the specific path and access it from your network class.
Also note, that is a better practice to access class variables with a getter-method.

What is a Type Literal in Java? [duplicate]

This question already has answers here:
Use of TypeLiteral in java
(5 answers)
Closed 9 years ago.
There is an ASTNode subclass called TypeLiteral.
The description of this type of code says that it looks like
( Type | void ) . class
I don't believe I have ever seen anything like this.
Can anyone provide an example of a TypeLiteral, and perhaps additionally of it being used in a Java program?
There is a class called Class that represents the class of an object. It contains various useful methods for introspection on that class and objects of that class. One way to get a class object is to use the Object method getClass(). But if you want to refer to a Class by name of its class, you use a class literal.
Some examples:
class Foo {
void method(Object b) {
if(b.getClass() == Foo.class) {
Foo f = Foo.class.cast(b);
}
Foo.class.getResourceAsStream(...);
Foo.class.getMethod("method", Object.class);
}
}

Calling non static array from static method [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java: how to call non static method from main method?
I am finding this a bit hard to implement.
I have a String[] called name which is declared globally.
String[]name;
Now i want to access this variable from the main() method. The main method is static therefore how could i access it.
I tried Animal.name but it didn't work.
How can i do this?
You need to create an instance of Animal class to access instance fields: -
Animal animal = new Animal();
animal.name; // Access array
You can solve this with two different ways, each requiring code modification:
First is to create an object of Animal type and accessing the name property.
Second is to make name as static.
like this: static String[] name = new String[10];

Categories