Solved

private static void main ?

Posted on 2000-05-18
5
378 Views
Last Modified: 2013-11-23
How does a main method work perfectly as a private function ?

Regards,
Ram
0
Comment
Question by:ramprasadr
5 Comments
 
LVL 3

Expert Comment

by:msmits
ID: 2821072
Shouldn't be any problem, but you will not be able to start your application via that function.
When the virtual machine loads your class to execute as an application it will not find the main function in the external interface.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 2821077
Well, according to the JVM specification, it shouldn't. But the JVM has intricate internal knowledge of all classes -- it has to. So the curent implementation of the JVM will find and invoke a private method main as long as it is static and its argument signature is correct. This is behavior that it is unwise to rely upon. The fact of Sun's own non-conforming JVM has been raised on the JDC forum and it is likely to change in the future.

Although I doubt that the JVM uses reflection to do this, in Java 2 it became possible for any class to see private members of other classes through reflection -- assuming that you have the security to do so. Using the invocation services offered by reflection, virtually anyone can call a private method on an object.

Best regards,
Jim Cakalic
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2822679

Hi JIM,

  Reflection package is listing only public methods.

0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2823058
ravindra,

Prior to Java 1.2 you were correct. However, 1.2 added "reflective access control". The reflection Field, Method, and Constructor objects now extend AccessibleObject through which even private members may now be declared to be accessible. There is also a ReflectPermission class that will accomplish the same thing. Here is an example of it's use:

---------- ReflectObject.java ----------
import java.io.*;
import java.util.Vector;
import java.lang.reflect.*;

public class ReflectObject {
    private byte b = 1;
    private short sh = 2;
    private int i = 3;
    private long l = 4;
    private String s = "I'm a string";
    private char ch = 'c';
    private Vector v = new Vector();
    private Object[] a = new Object[2];

    public ReflectObject() {
        a[0] = new String("answer to life, the universe, and everything");
        a[1] = new Integer(42);
        v.add(a[0]);
        v.add(a[1]);
    }

    private static void writeObject(Writer w, Object o, String id) throws IOException {
        if (o == null) {
            w.write("null object encountered\n");
            return;
        }

        Class clazz = o.getClass();
        Field[] fields = clazz.getDeclaredFields();
        AccessibleObject.setAccessible(fields, true);
        String name = clazz.getName();

        w.write(clazz.getName() + " " + id +" = { ");
        for (int i = 0; i < fields.length; i++) {
            try {
                String fieldName = fields[i].getName();
                Class fieldClass = fields[i].getType();
                String fieldType = fieldClass.getName();
                String fieldValue;

                if (i > 0) {
                    w.write(", ");
                }
                if (fieldClass.isPrimitive()) {
                    if (fieldType.equals("int"))
                        fieldValue = String.valueOf(fields[i].getInt(o));
                    else if (fieldType.equals("short")) {
                        fieldValue = String.valueOf(fields[i].getShort(o));
                    } else if (fieldType.equals("boolean")) {
                        fieldValue = String.valueOf(fields[i].getBoolean(o));
                    } else if (fieldType.equals("char")) {
                        fieldValue = String.valueOf(fields[i].getChar(o));
                    } else if (fieldType.equals("byte")) {
                        fieldValue = String.valueOf(fields[i].getByte(o));
                    } else if (fieldType.equals("long")) {
                        fieldValue = String.valueOf(fields[i].getLong(o));
                    } else if (fieldType.equals("float")) {
                        fieldValue = String.valueOf(fields[i].getFloat(o));
                    } else if (fieldType.equals("double")) {
                        fieldValue = String.valueOf(fields[i].getDouble(o));
                    } else if (fieldType.equals("void")) {
                        fieldValue = new String("(void values unavailable)");
                    } else {
                        fieldValue = new String(" no value or type available ");
                    }
                    w.write(fieldName + "=" + fieldValue);
                } else if (fieldType.equalsIgnoreCase("java.lang.String")) {
                    w.write(fieldName + "='" + fields[i].get(o) + "'");
                } else {
                    // contained object
                    writeObject(w, fields[i].get(o), fieldName);
                }
            } catch (IllegalAccessException iae) {
                iae.printStackTrace();
                break;
            }
        }
        w.write(" }\n");
    }

    public static void write(Writer w, Object o, String id) {
        try {
            if (o == null) {
                w.write("null object supplied\n");
                return;
            }
            writeObject(w, o, id);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        OutputStreamWriter w = new OutputStreamWriter(System.out);
        Object o = new ReflectObject();
        System.out.println(o);
        write(w, o, "o");
        try {
            w.flush();
        } catch (Exception e) {
        }
    }
}
---------- end ----------

Compile and invoke this on itself: "java ReflectObject ReflectObject" or any other class that contains private members. You will see that it is able to access private members of the class.

Best regards,
Jim Cakalic
0
 

Author Comment

by:ramprasadr
ID: 2825337
Thank you very much, Jim. I was asked this question in an interview.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 95
stackato and cloud 4 85
print map entry 34 67
Spring Framework HTTPSession management 1 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question