• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • Last Modified:

java bean related info

Hi,


why the bean always implements serilalizeable

public class StudentsBean implements java.io.Serializable

Do we need import statement also to get Serializable.

i have not understood what it mean by saving the object state at different instances to disk. what is disk what is state when we have to save its state.
is disc same as database.

what is persistence.
how persistence related to serialiazeable.

How beans help in component kind of programming.

How java beans are related or different from enterprise java beans.
0
gudii9
Asked:
gudii9
  • 11
  • 7
  • 7
  • +2
4 Solutions
 
CEHJCommented:
https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html

EJBs are related to 'Java Beans' - but not in any important way ;) There's no need for beans to be Serializable but they often are when persistence is necessary
0
 
gheistCommented:
Serializable is required for RMI, for the rest it just makes data easier to see in assembly debugger (java debugger will be fine either way)
It is kind of data layout, packing all parameters in order much like C++ does.
It is not required but can be used via HTTP interfaces or stored to disk. Normally one uses better defined formats like XML, that adds some packing and interpreting overhead, but are absolute must for communication with the world outside java (you will never know where C# hits you)
Some catches:
You will encounter unrelated serializable attribute in JTA transactions.
When updating serializable type in caller also callee must have same new format understood. You cannot even have one version field that defines if the tail has 5 integers in v1 and 4 integers + float in v2. XML or plain text will get over that easily.
0
 
dpearsonCommented:
i have not understood what it mean by saving the object state at different instances to disk. what is disk what is state when we have to save its state.
Saving to disk just means storing it in some way on that can be loaded later.  It literally means writing it to a disk somewhere.

is disc same as database.
Yes a database is one form of disk storage.

what is persistence.
Persistence just means storing something in a way that you can load it later - so it's just another way to say "storing to disk".

how persistence related to serialiazeable.
When something is serializable it means Java knows how to turn the object into a stream of characters and then turn that stream of characters back into an object.   So once you have a way to do that, then storing it persistently (i.e. on a disk) becomes very easy - you just write the characters to a file or into a field in a database and read those characters back later.

Hope that helps you understand this a bit more,

Doug
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
Java knows how to turn the object into a stream of characters
Just to clarify: that does not mean that what is produced is text. A stream of bytes in a special binary format is what the result is.  e.g

import java.io.*;


public class Bean implements Serializable {
    private String foo;
    private int bar;

    public Bean() {
    }

    public Bean(String foo, int bar) {
        this.foo = foo;
        this.bar = bar;
    }

    public String getFoo() {
        return this.foo;
    }

    public int getBar() {
        return this.bar;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public void setBar(int bar) {
        this.bar = bar;
    }

    public String toString() {
        return String.format("%s=%s,%s=%d", "foo", foo, "bar", bar);
    }

    public static void main(String[] args) throws Exception {
        Bean b = new Bean("foo", 100);
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                    "x.bin"));
        out.writeObject(b);
        out.close();
    }
}

Open in new window


The binary content of that file:

0000000: aced 0005 7372 0004 4265 616e dea9 1fbe  ....sr..Bean....
0000010: fa16 4958 0200 0249 0003 6261 724c 0003  ..IX...I..barL..
0000020: 666f 6f74 0012 4c6a 6176 612f 6c61 6e67  foot..Ljava/lang
0000030: 2f53 7472 696e 673b 7870 0000 0064 7400  /String;xp...dt.
0000040: 0366 6f6f                                .foo

Open in new window

0
 
gudii9Author Commented:
import java.io.*;


public class Bean implements Serializable {
    private String foo;
    private int bar;

    public Bean() {
    }

    public Bean(String foo, int bar) {
        this.foo = foo;
        this.bar = bar;
    }

    public String getFoo() {
        return this.foo;
    }

    public int getBar() {
        return this.bar;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public void setBar(int bar) {
        this.bar = bar;
    }

    public String toString() {
        return String.format("%s=%s,%s=%d", "foo", foo, "bar", bar);
    }

    public static void main(String[] args) throws Exception {
        Bean b = new Bean("foo", 100);
        System.out.println("test....");
      
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                    "x.bin"));
        out.writeObject(b);
        out.close();
    }
}

Open in new window


i tried as above i only see below output not binary content pleas advise

test....
0
 
gudii9Author Commented:
When something is serializable it means Java knows how to turn the object into a stream of characters and then turn that stream of characters back into an object.   So once you have a way to do that, then storing it persistently (i.e. on a disk) becomes very easy - you just write the characters to a file or into a field in a database and read those characters back later.
cloneable interface comes to picture at the destination right while reading after the object passed through network from source to destination folder?
please advise
0
 
gheistCommented:
binary content was written to x.bin file in your current directory
It has nothing to do with network support past passing java parameters over RMI
0
 
gudii9Author Commented:
binary content was written to x.bin file in your current directory

i am using eclipse to run this. Where can find x.bin fie. I do not see that created from eclipse though. Is it is a hidden file which is created in the workspace location. My Bean.java file is under below path

C:\Users\xyz\IBM\rationalsdp\upgraded_workspaces\test_workspace\TestJavaProject\src\Bean.java
0
 
gudii9Author Commented:
as attached i was not able to see x.bin from eclipse but when i go to file structure i can see it.

when i opened with notepad++  x.bin is not opening. Please advise on how to see the content on my windows laptop. Please advise
TestProjctBin.jpg
0
 
gheistCommented:
run program from command line outside eclipse...
0
 
gudii9Author Commented:
run program from command line outside eclipse...

for that i need to set up java and javac and all that. I wonder by using eclipse how to see x.bin file.

please advise
0
 
gudii9Author Commented:
javac is not recognized internal and esternal command. i do not want to install any software on this office laptop. please advise
0
 
gheistCommented:
How that relates to initial question? You must be able to understand what 1-page sized java code does without calling all devils of eclipse and group policies together.
0
 
gudii9Author Commented:
let me try from home laptop later on.
0
 
CEHJCommented:
as attached i was not able to see x.bin from eclipse but when i go to file structure i can see it.
So - that just tells us you need to know how to use Eclipse. It's actually not important anyway to be able to see it from Eclipse
0
 
gheistCommented:
You do not need to have javac to RUN java class file.
Eclipses unability to open serialized representation just illustrates how fragile the format is.
0
 
dpearsonCommented:
(This is why I originally left out the binary format and just described it as a string of characters - it's pretty confusing and not really key to understanding the concept behind serialization - although it does help if you want to delve into the details of the implementation).

Doug
0
 
CEHJCommented:
Eclipses unability to open serialized representation just illustrates how fragile the format is.
Well, i don't think the format is in any way fragile. All that shows is that Eclipse might not (out of the box at least) either support use as a binary editor or a viewer of serialized information

Update: there is a binary editor plugin (EHEP) for Eclipse, but from the POV of this question, examining the serialized file is probably overkill
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Just to continue off-topic (but hey, how many of these authors questions do stay on topic? ;)...

i am using eclipse to run this. Where can find x.bin fie. I do not see that created from eclipse though.
You would most likely need to tell Eclipse to refresh it view of the project directory structure, ie. click on the project in Eclipse "Project Explorer" view, and then press "F5" and "x.bin" should appear in the level under your project. As the others have mentioned, Eclipse still might not be able to open that binary file in any useful/readable format. But at least with it now viewable in the explorer, you can open it externally, etc.
0
 
gudii9Author Commented:
You would most likely need to tell Eclipse to refresh it view of the project directory structure, ie. click on the project in Eclipse "Project Explorer" view, and then press "F5" and "x.bin" should appear in the level under your project. As the others have mentioned, Eclipse still might not be able to open that binary file in any useful/readable format. But at least with it now viewable in the explorer, you can open it externally, etc.

when click F5 i see x.bin.

But as you mentioned it is not readable. How to open and read it externally. please advise
0
 
gheistCommented:
With notepad?
0
 
gudii9Author Commented:
when i open with notepad i ses
¬í sr BeanÞ©¾úIX I barL foot Ljava/lang/String;xp   dt foo

not sure why it is bit unreadable as above
notepad.png
0
 
gheistCommented:
Because it is in binary format native to JVM
0
 
CEHJCommented:
notepad++ has a binary editor plugin
0
 
gudii9Author Commented:
notepad++ has a binary editor plugin

what is the name of the plugin. How do i install it and view the content using that plug in. please advise
0
 
gudii9Author Commented:
i see one youtube videos. i will watch that and see if i can figure out that later. If not open new thead as that is different topic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 11
  • 7
  • 7
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now