aspose file tools*
The moose likes Beginning Java and the fly likes simple doubts, please explain. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple doubts, please explain." Watch "simple doubts, please explain." New topic
Author

simple doubts, please explain.

Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40


What does these kind of statements mean ? what is dynamic initialization.? This word dynamic, has confused me a lot in learning java. Any one please explain this mistry.This is mystery for me. May be easy for you guys, I want to make it easy formyself also.
Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40
one more doubt, lets say there is class 1 which has method 1. And there is class 2 which has some method. they are in same package. is it possible that i call Class2.method1. ?? i do not understand how ?
Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40
method is public.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
That is how you can call methods on objects of other classes.
As for the access modifiers, start here.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23


The word "dynamic" can have different meanings, but usually it means that something is checked / determined / happens at runtime, rather than at compile time (for that, the word "static" is used).

Look at the following example:

When you run the Main class, you can specify on the command line which class to create an instance of and to call run() on. For example, run it like this:

java Main One

Note that the compiler will not know, at the moment when you compile the program, what type of object runnable will be exactly - that will only be known at runtime (when you run the program). The compiler doesn't know what the dynamic type of runnable is. (It's static type is Runnable).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Kelly Powell
Ranch Hand

Joined: Oct 29, 2010
Posts: 56
Private and protected are examples of access modifiers. All in all, there are 4 types of access modifiers.

If a variable is private, it means that it is only accessible inside the class where it is declared.

If a variable is protected, it means that it is accessible not only inside the class where it is declared, but also to other classes which belongs to the same package as it is. A protected variable is also accessible to its subclasses.

You may visit the link provided by Campbell for the other access modifiers.

Dynamic initialization is when a variable's value is determined on runtime rather than on compile time.

For example,

The reason for this is because when the variables are compiled, the compiler already knows that the value of num1 is 2 and so it assigns the value 2 to num1. However, a compiler only compiles, but does not compute. When the compiler sees 1 + 1, instead of computing it and assigning the sum to num2, it assign 1 + 1 directly to num2 without computing it. Now, when num2 is run on the runtime, 1 + 1 will now be executed and it is the only time that the value of num2 which is 2 is determined.

Mezan Shareef wrote:one more doubt, lets say there is class 1 which has method 1. And there is class 2 which has some method. they are in same package. is it possible that i call Class2.method1. ?? i do not understand how ?


Yes, it is possible. In fact, you may access the methods of Class2 even though you're in a different package. One way to do it is what Campbell had shown. The other way is to declare the method as static. A great example for this are the methods of the Math class. Ever wonder why you can easily access the methods (e.g. Math.abs(param)) of the Math class without the need to declare a variable of type Math? It's because they are declared as static.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Kelly Powell wrote: . . . The other way is to declare the method as static. . . .
What has static got to do with access? You can gain access to members of a class from any package inside your classpath if those members are declared public, and from subclasses if they are protected. But static members are something completely different. It is worth making a method static if all the following conditions are fulfilled:
  • That method does not take any information from the state of the object (ie does not read any instance fields or any instance methods' return values).
  • That method does not change any of the state of the object (ie does not change any instance fields or call any instance methods).
  • Then it is worth making a method static. You should always call static members by the name of the class, not the name of the object. Math.sin() takes a number and returns its sine. You should not say something likeIn fact the compiler will not allow you to say new Math() because that constructor is private
    Kelly Powell
    Ranch Hand

    Joined: Oct 29, 2010
    Posts: 56
    @Cambell: Yes, static is not an access modifier. I am just stating that it is possible to access a class' method without the need to declare a variable of its type. I'm talking about the ways he can access a method not the access scope of a method.

    Mezan Shareef wrote:... is it possible that i call Class2.method1. ??...

    If he wish to access a method using its class name (e.g. ClassName.methodName()), then that method must be declared as static. Based on his question, I assumed that "Class2" is a class name. If that is the case, then "method1" must be declared as static. Clearly, he won't be able to access "method1" using "Class2.method1()" if "method1" is not static.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    Never go round thinking, "if you want to access a member from the class name . . ." You can think, "this method never accesses instance methods, so it can be static," however.

    If you have justification for making a member static, then you can use ClassName.memberName.
    Robin John
    Ranch Hand

    Joined: Sep 10, 2008
    Posts: 270

    Mezan Shareef wrote:one more doubt, lets say there is class 1 which has method 1. And there is class 2 which has some method. they are in same package. is it possible that i call Class2.method1. ?? i do not understand how ?


    If you dont have a typo and you dont have a mistake in this question and if I am reading this correctly... you cannot access the method 1 by Class2 name or instance, method 1 is a member of Class1 and you can only access it using a class1 instance or by 'Class1' name as previously mentioned...


    Time is what we want the most, but what we use the worst. -- William Penn
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: simple doubts, please explain.