Win a copy of Event Streams in Action this week in the Java in General forum!

Hari Nagarjuna

Ranch Hand
+ Follow
since Jun 11, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
28
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Hari Nagarjuna

I got this error in a MIDI code
Jun 22, 2019 9:27:23 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.

The code is
1 month ago

Paul Clapham wrote:
The getContentPane() method returns a reference to some object. So what is calling add(button)? That object is.



Considering there is a reference returned where is it stored in our class?
For example

Should work because a is copying/storing the returned reference.
[This example is under the assumption that the reference returned is from a class that has JFrame in it's inheritance tree and I do not know the object of which class was returned ]
In the code below


There is no copy of returned reference and we should not be able to use the reference returned directly because it
1.is not a reference of this class(out of scope).
2.It's life should end as it's method should already be popped of the stack as the method has already returned a value.
Please correct me if I am wrong.
2 months ago

Randy Tong wrote:

Hari Nagarjuna wrote:What is happening in System. out. println()??


dot(s) used to separate class name, field and method.

In this case, out is a class variable of type PrintStream which declared in the System class, while println methods is in class PrintStream.

So we need to use System.out.println to print something out in console.





This is what I understood.

Here System.out works because out is STATIC variable of class System

Out is reference variable of class Printstream so it can be used to reference/call methods of Printstream including println().

But for this first an object of Printstream class should be created??

So the code should be



Please correct me if I am wrong.
2 months ago


What is happening here?
panel is reference of JFrame so it can use dot operator to reference getContentPane method.
So what is referencing/calling add(button)??

What is happening in System. out. println()??
2 months ago
import java.lang.Math

Means class Math from lang java package should have been imported and its methods are available for use.

import static java.lang.Math. *

What is the difference between these two?

2 months ago

Paul Clapham wrote:

Hari Nagarjuna wrote:Yes. I understand that it is a static method.
But why are we assigning a method to a object reference variable of calendar(c)?
Can't we only assign objects(Objects may be of its subclasses in case of using polymorphism) to object reference variables??



Oh right, it's that question. Yes, you have a reference variable of type Calendar. And you're assigning to that variable a reference to some object. So the object must be assignable to a variable of type Calendar. But that doesn't mean it has to be an object of the Calendar class. It can be an object of some non-abstract subclass of Calendar... which is what it is.

Try writing another line of code which tells you what class that object actually belongs to.



When I called getclass method on reference variable c it gave me

Java. util. GregorianCalendar

Which should mean an object of GregorianCalendar should have been created and assigned to reference variable c of Calendar class.(This is under the speculation that GregorianCalendar is sub class of Calendar)

But I have never written the code


So how did this happen?
2 months ago

Paul Clapham wrote:It calls a static method from the Calendar class.

True, Calendar is an abstract class and you can't create an object of the Calendar class, but as you know by now, a static method doesn't need a method for it to be called.

You can tell it's a static method because the name of the method is prefixed by the name of the class.



Yes. I understand that it is a static method.
But why are we assigning a method to a object reference variable of calendar(c)?
Can't we only assign objects(Objects may be of its subclasses in case of using polymorphism) to object reference variables??
2 months ago

What does this do? Considering Calendar is an abstract class and can't have any objects?
2 months ago

Junilu Lacar wrote:No, an interface does NOT inherit from Object, or more correctly, it is not a subtype of the Object class. Think of an interface as a description of some kind of capability. You don't instantiate interfaces; you instantiate objects.

Try running this code:



Thanks for your reply.
It is showing null for Foo.class.getSuperclass() but
java. lang. Object for Bar.class.getSuperclass()

Which means Interfaces do not inherit class Object.
2 months ago

Stephan van Hulst wrote:

Brecht Geeraerts wrote:All interface methods are implicitly public (if I am not mistaken)


If you don't use an access modifier the method will be public. Since Java 9, you can also declare private methods in an interface which like default and static methods can not be abstract.



So does interface inherit from class Object or not since Interface is not a class??
2 months ago
Does interfaces inherit from Object class?? (Interfaces methods should be public and abstract by default)
2 months ago

Campbell Ritchie wrote:

Les Morgan wrote:. . . If we type int[3] what we have created is a reference that will point to 3 integers, but it is still only the variable that will reference the ints, we have not constructed an array yet--just a referencing scheme. . . .

Can you write int[3] as a type? I thought you can only use the number on the right of the assignment operator:

. . . you typed in a literal array/collection of ints. . . .

That is called an array initialiser. Unlike writing new int[3], which creates an array filled with 0s/nulls/falses, an initialiser creates an array filled with the actual values desired. Code like...both declares the reference and creates the array object in one line.



Thanks for your reply.
So there is no special purpose in using

apart from initializing the array elements to 0's/null's/false's?
as object can be created directly using

2 months ago

Les Morgan wrote:Hari,

I am not sure I understand completely what you are asking, but let me see if I am getting a piece of what you are saying.

When we "create an array" in Java and many other languages, I think what you are referring to is the left side of the expression: int[] in the case we wish to build an integer array.  When we type int[], there is no array created, but instead what we have made is a reference that will be set to reference an array object.  If we type int[3] what we have created is a reference that will point to 3 integers, but it is still only the variable that will reference the ints, we have not constructed an array yet--just a referencing scheme.



Yes this is exactly where I am confused.
in
I have just created a reference.
But I have not created an actual array object using

which should create create integer array object of required space in which we can give our input.
But in

There is no array object created .Reference just points to the object created and will not store any data given by us.
So where integers 1,2,3 stored since there is no object created.


2 months ago
Array is an object;So why can we create an array and assign its elements without creating space for it?

Example-




For objects-



In the above code,
pen is an object reference variable or object name,
while actual object is created using new Item()


2 months ago