Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!

Masa Saito

Greenhorn
+ Follow
since Jun 08, 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 Masa Saito

Rajiv Chelsea wrote:Why cant consructors be marked final and absract?


Constructors are not methods, and that is the fundamental answer.
The term "abstract" is only relevant to the idea of "polymorphism": (run-time vs. compile-time decision making). So consider how construction works:

"Object obj = new Whatever(x, y, z);"

As you can see, at compile-time, we already know an instance of "Whatever" is created. There is no dynamism here. Which constructor to use? Well.. its the constructor for "Whatever".

Further, the fathers of Java only want a constructor to initialize the state of an object. Set the instance variables to values that allow the object to work correctly. So, a class can say: "I need this constructor with those variables to initialize myself and be ready to rock and roll". But why would it make sense for a class to tell its subclasses which variables it needs to initialize themselves (ie. an abstract constructor)? A class knows nothing about its subclasses.

Finally, constructors are not "inherited" in the normal way people talk about inheritance because they are not methods. Constructors are "hidden" not "overrridden", by subclasses, so "final" does not make sense in a constructor. Using the "super" keyword, you do have access to public and protected constructors in your parent class.

If you could give some very specific examples of what you are asking about, that would help the discussion.
8 years ago

Rob Prime wrote:The disadvantage of using those is that you'll need to store the entire byte[] into memory.


That is an interesting technique.
At the least, I'd test about buffering the streams and messing around with the buffer sizes.
8 years ago

Anna Smalska wrote:Hello,
Using pipedoutputstream and pipedinputstream is the best way of converting outputstream into inputstream,


What do you mean by this? What do you want to do?
I can almost guarantee you want to use ByteArrayOutputStream / ByteArrayInputStream. Pipes are rarely used.
8 years ago
Using Serialization over piped streams I believe I can perform deep copy (cloning).
Is this correct? Here is a code snippet:

Twisting together piped streams in the same thread is prone to deadlock as per javadoc.
But that aside, "cloneObj" is a clone of "originalObj" in 100% everyway, right?
Should this be considered a valid technique in production code? If not, why not?
A senior developer informed me that the fathers of Java intended serialization to only be used to persist an object outside the jvm. Using the twisted pipes technique was wrong because it violates the "intended use" of serialization and he refused to discuss any further.

What says the Java Ranch? Serialization is deep copy. Clone is deep copy. I can serialize to perform cloning. right?
8 years ago

Campbell Ritchie wrote:

André Asantos wrote:Hi Masa,
thanks a lot for you attention... But sorry I did not understand your answer...

Nor did I.


As the class implements Serializable, and and open Socket is "stateful" the Socket would need to be transient.
Writing to the socket's stream would require synchronization (that there are two methods that write is not relevant). the write and the incriment of "x" needs to be atomic. sync-ing only on the output stream would be best. To implement the Factory pattern, you can't have public constructors so you'd need to make the constructor protected. the subclasses would need to have the static factory methods. as static methods can't be abstract, i had to write in the comments the class needs extending.

anyway. take care.
8 years ago

André Asantos wrote:Which situations are possible the use of transient, synchronized, native and protected modifiers?

thanks,
AS


here is a situation where you need: transient, synchronized, and protected. [but not "native"]
8 years ago

Rahul Nair wrote:
Hi Rajesh,

Thank you very much for solution. I want to know... can we load User defined classes using the primordial class loader and Standard java classes using User Defined Class Loader ? I mean is it permissible ?


In a case like this, you need to write test code. Run it and let the jvm tell you if it is "permissible". If you have trouble writing test code, ask for help again about errors in your test code.

I have a working knowledge of custom classloaders and want to help, but there is no way I will offer any opinions about this without running test code. But I don't want to help enough to write test code some i'm not saying nothing. There are many highly knowledgable people on this forum that have helped me a lot. But, in my opinion, the results of test code trumps everything. The jvm is always correct, and you can ask the jvm if something is "permissible" by running test code.
8 years ago

James Sabre wrote:

Masa Saito wrote:This is the most platform independent way to handle new lines:

public static final String NEW_LINE = System.getProperty("line.separator");


That techniques asks the OS "hey this is the jvm. how do you start a new line in a string in your environment?" and so no need to hardwire any characters "\n" or "\r" or otherwise.



Sorry but that is not platform independent. The end-of-line character(s) depend on where (or how) the file is created and not where the file is processed. If I create the file on *nix using a standard *nix editor, transfer in binary mode to Windows then the end-of-line character is not System.getProperty("line.separator"); .

There is no truly platform independent approach but I would use the heretical approach of using a regex looking for "\r\n" or "\r" or "\n" .


I see your point... you just helped me find a bug in someone else's source I've been looking at.

But, now that I think about it, this is what I really meant:

and that will always give me the correct newline character (as long as I stay on the same platform). Doing a "\n" hardwire will cause different results in "hello.txt" based on platform.
8 years ago

krishna Karthikk wrote:
If my class has to be initialized,


A class (as opposed to an instance of a class), can be initialized only once. It happens in the "static initializer". Instances of a class are initialized in a constructor. Here is short code that demonstrates what happens:


krishna Karthikk wrote:then the main method should be accessed using an object.


The object is the "bootstrap thread" as already mentioned and clearly written in the javadoc for java.lang.Thread . Please do some work and read javadoc.

krishna Karthikk wrote:If there are two instances of main method


At this point, its best not to think of methods being objects. But if your friend wants to, just tell him he is 101% wrong, and tell him to look at the reflective api.

krishna Karthikk wrote: the jvm will confuse. If the main method is declared as static then only one single copy of main exists.



At this point, you will need to post Java code that shows exactly what you are talking about.
8 years ago
This is the most platform independent way to handle new lines:

public static final String NEW_LINE = System.getProperty("line.separator");


That techniques asks the OS "hey this is the jvm. how do you start a new line in a string in your environment?" and so no need to hardwire any characters "\n" or "\r" or otherwise.
8 years ago

Jim Hoglund wrote:

Jim Hoglund wrote:... non-static code operates only on instance variables. ...


Quoting my error here, (I was rushing to leave for dinner) this certainly is not true.
Non-static code has access to ALL its class members. Is it correct to say that labeling
a method static restricts its access to only other static members?. The main purpose
of my post was to address duplication of method code.

Jim ... ...


As best as I know, that is correct.
But fields and nested classes in implemented interfaces are also accessible from static methods.


8 years ago

Jim Hoglund wrote:Each object has state as stored in its assigned copy of instance variables. The class
itself also has state that's stored in the class (static) variables. The JVM loads just
one copy of each constructor, method and initialization block. Static code can operate
only on the class variables while non-static code operates only on instance variables.
......


That is not quite true.
An instance has access to its class, however a class doesn't have access to its instances. Thus, instances can change the state of their class, but a class cannot change the state of any of its instances. Example:



Using an instance of "Main" I changed the static state of Main. The reason I used the reflection is to emphasize all methods (instance and static) belong to classes. I got the method "register()" from the "Main" class, then "register()" was "invoked on" an instance of Main. anyway, this discussion went far afield from what the OP wanted to know. sorry. i'm new and just want to contribute....
8 years ago

krishna Karthikk wrote:Hi everybody, I am Krishna, suddenly I got a doubt, why main function is declared as static. I know that java calls the main function using the class name. If a method has to be called using class name it must be declared as static. So we declare main as static while coding.

The java developers can initialize my java class and call the main method using an object. Why they designed the architecture in this way. Is there any specific reason. One of my friend said that if we declare a function as static it will have only one copy n the memory and something. I did not understand that.

Can anyone tell me why main is declared as static?


just look at the javadoc for java.lang.Thread . on the 3rd paragraph:

"When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:...."
Running this code should emphasize what is happening.

Notice that the "main(String[] args)" is a normal static method. The bootstrap thread is just hardwired to invoke the method signature "static void main(String[] args)" to get things rolling.

"One of my friend said that if we declare a function as static it will have only one copy n the memory"
Your friend is wrong. There is always only one copy of a method (regardless of if its static or not). Don't think of an object "having methods". Rather, think of methods performing operations on objects.
8 years ago
Regarding abstract static methods (as well as why interfaces can't have static methods), does not it all come down to method binding? Static methods, by design intent, are not to be associated with instances. So, I am betting the fathers of Java decided to go ahead and bind at compile time. As abstract methods have no known implementations at compile-time, there is nothing to bind to. I bet it was just a design decision to disallow "static polymorphism". Those are contradictory adjectives and concepts.
8 years ago