Philip Mat

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

Recent posts by Philip Mat

I think I found the answer to my problem..

When we hide a variable in sub class, there will be 2 different variables (in 2 memory locations), one for super class and one for sub class.

When we don't hide a variable in sub class, there is one and only one variable (in 1 memory location) shared by both super class and sub class.

The method: toString() is not overridden in sub class B. So the version from super class A is executed.

In the first case when the variable: string is not hidden, the statement: b.string = "c"; changes the one and only one variable: string that is shared by super class A and sub class B. So when toString() method is called the value of variable: string will be "c".

In the second case when the variable: string is hidden, there will be 2 different variables named string and the statement: b.string = "c"; changes the one in sub class B. So when toString() method is called the value of variable: string will be "a".

6 years ago

Henry Wong wrote:

So, when you print the variable, which method is used to access the string variable? and what is the type of the reference that is used to access the string variable?

Henry



This is what my understanding is.

Since the toString() method is not overridden in class B, the one in class A gets executed even though the type of the reference that is used to access toString() method is B. And the value of variable string will be "a".

What I don't understand is the following behaviour.

If the variable string in B is not hidden, the statement: b.string = "c"; takes effect and the value of variable string will be "c".

If the variable string in B is hidden, the statement: b.string = "c"; is ignored and the value of variable string will be "a".
6 years ago
I have difficulty understanding the following behaviour.


The output of the program is 'c'. This is expexted behaviour. But if class B is changed as follows,


Now the program prints 'a' instead of 'c'. Why the statement: b.string = "c"; is not taken into account? Thanks in advance..
6 years ago
Oops... That was a stupid mistake from my side. Thanks a lot for replying...
10 years ago
Hi,

I'm using the following code in my windows 7 machine. The list() method is not working.

It's showing messages: "Is a directory !!" and "Path: C:\Java Projects\src". But the fileList array's length is zero. It's printing "List array is empty !!" in the log file.

There are files and sub directories inside C:\Java Projects\src. What could be wrong?



import java.io.*;

public class FileSearch {

public static void main(String[] args) {

try {

File dir = new File("C:\\Java Projects\\src");

if (!dir.isDirectory()) { System.out.println("Not a directory !!"); }
else { System.out.println("Is a directory !!"); }

System.out.println("Path: " + dir.getPath());

File logFile = new File("C:\\temp\\log.txt");

String[] fileList = dir.list();

PrintWriter pw = new PrintWriter(logFile);

if (fileList.length == 0) { for (String s : fileList) { pw.println(s); } }
else { pw.println("List array is empty !!"); }

pw.flush(); pw.close();


} catch (IOException ex) { System.out.println("IOException caught:- " + ex.toString()); }


}

}
10 years ago
Thanks all for your replies.
11 years ago
But still I need to hard code the file path for the configuration file in the program, right?
11 years ago
I'm creating a monthly planner program in java swing. I'm planning to use a file to serialize the objects. Right now, I've hard coded the file path in the program.
What I want to do later is to ask the user to provide a file path and use that file path for serialization.
But I don't know how to retain or embed that file path in the program. When the user exits the program, the file path will be lost and next time when the program starts, it doesn't know where to find the old serialized file.
Can you help?
11 years ago
Thanks .. It worked ..
11 years ago
Hi

Thank you for that information. How can I include a folder structure in a jar file?

Can you show me an example?
11 years ago
Hi

I'm new to java and I'm trying to make an executable jar file with no luck so far. My OS is windows xp.

I have my compiled class file: GuiDemo.class located at C:\Java Projects\bin\com\test\common. I created a manifest file: GuiDemo.mf at the same location which has the below contents and a blank line at the end of the manifest file.

Manifest-Version: 1.0
Main-Class: com.test.common.GuiDemo

I created the executable jar file like below at command prompt:

C:\Java Projects\bin\com\test\common>jar cmf GuiDemo.mf GuiDemo.jar GuiDemo.class

The above command did create an executable jar file: GuiDemo.jar, which I tested using C:\Java Projects\bin\com\test\common>jar -tf GuiDemo.jar and it gave the following result.

META-INF/
META-INF/MANIFEST.MF
GuiDemo.class

But, when I try to execute the jar file using: C:\Java Projects\bin\com\test\common>java -jar GuiDemo.jar, it is giving NoClassDefFound exception and the last line says: Could not find the main class: com.test.common.GuiDemo.class. Program will exit.

My CLASSPATH is set as C:\Java Projects\bin. I can execute GuiDemo without any issues using C:\>java com.test.common.GuiDemo

What could be wrong?



11 years ago
Thank you ..
11 years ago
I'm new to Java and I have the below code which when compiled gives the error:- unreported exception java.io.IOexception; must be caught or declared to be thrown.
Since I'm catching the IOException in my code, I think I don't really need to declare the IOException to be thrown on main method. It compiles successfully only when I declare the IOException to be thrown on main method.

What can be done to make this code compile successfully without declaring the IOException to be thrown on main method?
11 years ago