|
Hello friend...
Just add this line in main()
System.out.println("aRef's type is " + aRef.getClass().toString());
and output for this line must me : aRef's type is b
when you call aRef.show() then first this call must be resolved by class of aRef's object if fuction show() not found then jvm try to resolved it from super class.
|
|
|
|
|
in java a static method can access only other static member .
But,how static main method access non-static member of other class....?
Please tell me... sir/mam
|
|
|
|
|
do not. That's bad style.
If you need static final values, put them in an Interface:
public interface IInterface {
public class Def{
public static final String
VALUE1= "Value1",
VALUE2 = "Value2";
}
}
You can then access these values like IInterface.Def.VALUE1
EDIT:
The main-class should instance a Object. Not much more to do in the main, leave the rest to the new Object:
public static void main(String[] args) {
new MyObject();
}
regards Torsten
I never finish anyth...
|
|
|
|
|
I don't like the style you've shown here for the main method. It means that your entire application is running within the scope of MyObject's contructor, which just always feels wrong to me. A constructor is there to construct an object and get it ready to do work, not to do the actual work.
I would have two classes:
public class MyApp {
public static void main(String[] args) {
MyWorker w = new MyWorker();
w.run();
}
}
public class MyWorker implements Runnable {
public void run() {
}
}
That way MyWorker can be regarded as a reusable service, and the startup code is completely separate.
|
|
|
|
|
I agree on that - luckily now it's up to you to explain all about Runnable and Thread
regards Torsten
I never finish anyth...
|
|
|
|
|
If you have a non-static member variable and the access modifier of the class allows that member to be exposed then you must have an instance of the class to access it.
|
|
|
|
|
A static member of a class is constrained to access only the static member of the same class. It can, however, access any non-static member (if it is accessible from outside) of any class (including the class in which it is defined) if it has an instance variable to access the non-static member.
|
|
|
|
|
Actually non-static members are accessed via an object. An static member function can access non-static member of it's class also but through an oject... like--
class TestClass{<br />
<br />
int i_m_belongs_to_an_object_of_TestClass;
<br />
static int i_m_belongs_to_TestClass;
<br />
public static void function(){<br />
i_m_belongs_to_TestClass = 0;<br />
<br />
TestClass obj = new TestClass();<br />
obj.i_m_belongs_to_an_object_of_TestClass = 0;
<br />
OtherClass objOther = new OtherClass();<br />
objOther.i_m_belongs_to_an_object_of_OtherClass = 0;
<br />
int someInt = OtherClass.i_m_belongs_to_OtherClass;
}<br />
}<br />
<br />
class OtherClass<br />
{<br />
int i_m_belongs_to_an_object_of_OtherClass;
<br />
static int i_m_belongs_to_OtherClass;
}code><br />
<br />
Only one copy is allocated for static data members, one copy per object for non-static data members
|
|
|
|
|
sir/mam
i need to know the working of join() ....
i tried books and net but i didn't got it.
Please tell me exactly why we use join() function and what happens when we call the join()..
Thanks
|
|
|
|
|
|
I tried to follow this link and got a security warning that the website was trying to open a program on my computer. No idea what it was or why, but that sort of thing makes me nervous.
|
|
|
|
|
no thread here.
regards Torsten
I never finish anyth...
|
|
|
|
|
To be fair, the javadoc for this method isn't very clear:
Waits for this thread to die.
That's it. Not the most helpful javadoc I've ever seen.
The Oracle Java tutorial[^] is better:
The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,
t.join();
causes the current thread to pause execution until t's thread terminates.
I think that's quite clear and succinct.
|
|
|
|
|
thanks david..it helps alot.
|
|
|
|
|
To ensure that a thread (referenced by some reference say threadRef) is finished, then you join() on that reference. like threadRef.join() if that thread is finished, then join() returned immediately otherwise join() will blocked until that thread get finished.
For example if you create three thread (a, b, c) for doing three different task A, B, C, from your main thread, but you need some initialization processing (which must be executed before start-up of a, b, c) and shutdown processing (which must be executed after finishing of a, b, c) then you may code like this
// in main()
System.out.println("System started");
initialization(); // a, b, c are not running yet.
System.out.println("initialization complete ");
Thread a = new Thread(new Task_A_RunnableClass());
Thread b = new Thread(new Task_B_RunnableClass());
Thread c = new Thread(new Task_C_RunnableClass());
a.start();
System.out.println("Thread "a" started ");
b.start();
System.out.println("Thread "b" started ");
c.start();
System.out.println("Thread "c" started ");
//Now wait for all thread to finished.
a.join();
System.out.println("Thread "a" finiished ");
b.join();
System.out.println("Thread "b" finiished ");
c.join();
System.out.println("Thread "c" finiished ");
shutdown(); // a, b, c are finished now.
System.out.println("System shutdown..... ");
|
|
|
|
|
Hi all,
I have page on which data is loaded from database it takes more time to load the page
so i want to display the loading ..... image which will intimate user to wait.
Any suggestions are welcome
Thanks & Regards
Yogesh
|
|
|
|
|
You mean like an image showing the machine is idle?
How about an dialog saying "wait"?
Why does it take so long to load the data?
regards Torsten
I never finish anyth...
|
|
|
|
|
The proper way to do this is with some form of progress bar[^].
The best things in life are not things.
|
|
|
|
|
Hello,
I would like to fully understand the concept of 'unchecked casting'.
I get an 'unchecked cast' warning when I try to do this:
<br />
List<Element> freq1;<br />
pfreq1 = XPath.newInstance("/values/val[@freq<10]");<br />
freq1 = (List<Element>) pfreq1.selectNodes(doc);
What I am doing on line 3 is converting a List (pfreq1) to a List<Element> (freq1).
After googling, I have taken over the following 'cure', which worked:
<br />
private void makeValid(List li) {<br />
freq1 = new ArrayList();<br />
for (Object o : li) {<br />
freq1.add((Element) o);<br />
}<br />
}<br />
<br />
makeValid(pfreq1.selectNodes(doc));<br />
This works, but I do not know why. I do not know why the 'unchecked cast' warning disappears then.
I am explicitly converting each element of the generic List pfreq1 to an Element, thus making all elements in that generic List turn into real Element objects, which would make it a List<Element>. Now the compiler is sure that every object inside that List is now of type Element and thus can convert it safely to a List<Element>. That's how I see it.
However, what I would like to know is how and why unchecked casting works.
Thank you
|
|
|
|
|
The return type of the selectNodes method on XPath is a plain List, not a List<Element>.
In line 3 you are telling the compiler that you want to cast the List returned from selectNodes to a List<Element>.
The problem comes when you think about what is inside that list. selectNodes has returned you a plain List, which could in theory contain any type of Object. So although you can tell the compiler to cast the List to a List<Element> there is no guarantee that the contents of the List will actually be of type Element when the thing runs.
So the unchecked cast warning is the compiler telling you to be careful: you want to cast this to a List of Elements, but you may not get a List of Elements at this point at runtime. There is no way the compiler can check that for you, so it is prompting you to say "Are you sure this is right?"
In the second case, you are taking a plain List from selectNodes, and then iterating over the contents and casting each entry in turn into an Element before adding it into the list. That makes sure that the entries being added into freq1 are the correct type (Element). So the compiler doesn't need to warn you that the entries may be the wrong type because you have taken care of that yourself.
Hopefully that helps to make things a bit clearer.
|
|
|
|
|
Hi guyz,
i had developed a chat server in java for my website and now i wish to use it for my wesbite for live chatting with customers,please help me on that(all i know is that it would require jsp\servlets and tomcat server )
thnx.
I THINK THE INFO GIVEN BY ME WAS INSUFFICIENT,
so lemme put it this way:
i was supposed to make a chat server which could be used for live chatting(to connect operator and client) on the website,
i modified one of the chat servers(which i took from this website) and now the chat server is ready(with both the server part and the client part),i have converted them into jar files and now i have to link that program to my website so that people who visit the website can chat directly with the operator on a single click).
for that i guess i need to make jsp and servlets..for that i need guidance that how to do that..
in short:I WAS SUPPOSED TO ENABLE LIVE CHAT IN A WEBSITE AS A PART OF MY PROJECT AND FOR THAT I HAVE MADE UP THE CHAT SERVER PROGRAM AND NOW I NEED HELP REGARDING HOW TO USE(LINK) IT ON MY WEBSITE
i hope i made myself clear..please ignore any typing mistakes,
thanx
modified on Wednesday, July 6, 2011 8:49 AM
|
|
|
|
|
There are lots of samples of this type of website around the internet. I'm sure a Google search for "java chat website" will yield some useful results.
The best things in life are not things.
|
|
|
|
|
sir,thankyou for your reply..please read the modified message once again
|
|
|
|
|
Member 8011053 wrote: i guess i need to make jsp and servlets..for that i need guidance that how to do that.
There is no simple one line answer to this question. If you do not have any experience in these technologies then you are not going to be able to complete this project easily. There are many samples around the internet and, once again, I suggest you go and research some of them.
The best things in life are not things.
|
|
|
|
|
ok thankyou for your advices , i have 1 month with me so i will try my best.
|
|
|
|