Solved

private static void main ?

Posted on 2000-05-18
5
362 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
Problem to start Neon 20 89
github account with ecipse 1 42
servlet example issue 6 33
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now