Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: forName() method

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default forName() method

    As I thought I understood it, the forName() method instantiates a class listed inside the parenthesis of the method.
    ex. Ben = Class.forName("nameBen");

    I'm confused when I see an example where the argument passed to the method is an arguments[] array with 0 slots.

    ben = Class.forName(arguments[0]);

    So far in the code the only array would be in the run method at main(String[] arguments) {

    I don't really understand what is happening when "arguments[0]" is being given to the forName method.

    *I was thinking it might be referring to the first argument in the class which maybe would be the Class variable: Class ben;
    but then why would not just the name of that class variable be used?

    src coude looks like this

    import java.lang.reflect.*;

    public class AppFinder {
    public static void main(String[] arguments) {
    if (arguments.length == 1) {
    Class ben;
    try {
    boolean isApplication = false;
    ben = Class.forName(arguments[0]);
    Last edited by JavaBenGinner; October 4th, 2012 at 11:50 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: forName() method

    Using your favorite search engine, try:
    using command line arguments in java

    If you have questions after some research feel free to post again.

    Basically in:
    ben = Class.forName(arguments[0]);
    the:
    arguments[0]
    is referring to the first element of:
    String[] arguments
    in:
    public static void main(String[] arguments) {
    such that the first command line argument is assigned to the variable ben

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    United Kingdom
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: forName() method

    Usually, a class can be used loaded implicitly on an on-demand basis when you create the first object of the class using the "new" operator.
    But you can also use Class.forName() methods to load a class expplicitly:
    • static Class<?> forName(String className) - Returns the Class object associated with the class or interface with the given string name.
    • static Class<?> forName(String name, boolean initialize, ClassLoader loader) - Returns the Class object associated with the class or interface with the given string name, using the given class loader.

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  3. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  4. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  5. How do you use class.forname?
    By mydarkpassenger in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 20th, 2010, 01:00 AM

Tags for this Thread