aspose file tools*
The moose likes Beginning Java and the fly likes order of execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

order of execution

sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jesper de Jong wrote:
If you don't understand exactly what the difference is between static and instance variables, then study Understanding Instance and Class Members in Oracle's Java Tutorials.

good to see you,ha thanks ,actually my doubts is just how default value will print for instance variable thats enough,now i understand
Jeff Verdegan wrote:

That's what "default" means. It means the value is there even if we don't explicitly set it.


thanks ok i got

Jeff Verdegan wrote:
No. I cannot explain it any more clearly than that. I don't know how to explain "the first time the class is used" more clearly than "the first time the class is used." If you did not understand that, I don't think there is anything I can say that you will understand.

And what part of it did you not understand anyway? You've been doing this the entire thread. Someone gives you a detailed explanation and you ask for more, but you don't provide any feedback and what you didn't understand. That makes it difficult to help you, and can lead to frustration for those who are trying to help.

i mean i understand whenever class is loaded first then it static variable and static block will initialized automatically without creating object for class and acess directly, small examples which is easily to understand thats why iam asking,k sorry for frustration ,whenever if we compile and run the program class is loaded first after that static variable and static method after that what are the loading (constructors,methods,instance variables,instance block,statments)like the way which order will comes? what are the loaded first and how it allocates or occupy memory in heap or stack for variables,methods and objects
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Section 12.4 and 12.5 of the Java Language Specification explains exactly in what order things are initialized when a class is loaded and when an object is created. (Beware, the JLS is not always easy to read - but it is the definitive reference if you want to know exactly how Java works).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jesper de Jong wrote:Section 12.4 and 12.5 of the Java Language Specification explains exactly in what order things are initialized when a class is loaded and when an object is created. (Beware, the JLS is not always easy to read - but it is the definitive reference if you want to know exactly how Java works).

yeah its very difficult to read and understand
here below progam compile time error whats the problem even i created object and call it,i think its a nested class or local class?

Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
2 errors
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
why there is no reply
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

sekhar kiran wrote:
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
2 errors

What do you think there is wrong with the code above? What do you think the error messages mean?

What's the purpose of the class AX? Why did you define it inside the main() method?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
And why are the declaration and the print instruction inside the local class?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7800
    
  21

sekhar kiran wrote:why there is no reply

Perhaps because nobody has anything to say, or because we're getting a bit frustrated with the fact that, after over 100 posts, this thread doesn't seem to be going anywhere.

Anyway, we're all volunteers here, not your personal answer service; so please EaseUp (←click).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jesper de Jong wrote:
sekhar kiran wrote:
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
Myclass.java:9: <identifier> expected
System.out.println(obj.a);
^
2 errors

What do you think there is wrong with the code above? What do you think the error messages mean?

What's the purpose of the class AX? Why did you define it inside the main() method?

i think we dont have to declare another class in main method,iam not understanding about error message,declaring class in a method which refere to local class so thats why i tried here
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Winston Gutkowski wrote:
sekhar kiran wrote:why there is no reply

Perhaps because nobody has anything to say, or because we're getting a bit frustrated with the fact that, after over 100 posts, this thread doesn't seem to be going anywhere.

Anyway, we're all volunteers here, not your personal answer service; so please EaseUp (←click).

Winston

i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7800
    
  21

sekhar kiran wrote:i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?

Because:
(a) Your reply shows just how much notice you take of responses (this is my 4th).
(b) What am I supposed to reply to? This thread has been meandering for the best part of a month; and I don't see any end in sight.

Why don't you simply state exactly what your problem is, and close threads when you get an answer?

Given the subject of this one, I can imagine it being open for as long as you've got "doubt in some program", which is NOT the intent of this site.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

sekhar kiran wrote:i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?

Even if someone hasn't posted, that doesn't mean they haven't been following it. I read a lot of threads that I don't post in because someone else has already said what I would say.

It seems like you are just typing various random statements in random places, and when it doesn't work, asking folks here why. And then you got upset when nobody answered you quickly enough. At least, that's how it looks to us.

I would suggest you get a good, basic book, and go through it. Just about everything you have asked would be covered in the first few chapters. Things like default values, and where you can place executable statements or define new classes.

This is not the hard stuff to learn on your own. We generally expect everyone to make SOME effort to learn on their own.

Please try.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

sekhar kiran wrote:i think we dont have to declare another class in main method,iam not understanding about error message,declaring class in a method which refere to local class so thats why i tried here

Arbitrary statements, such as System.out.println(...), must always be in a method or constructor (or inside a static or instance initializer). You cannot put statements at class-level, which is what you were doing in your example. It doesn't matter that class AX is defined inside the main() method; the System.out.println(...) statement is not inside a method or constructor (or an initializer) in the AX class, so it's not allowed there.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Winston Gutkowski wrote:
sekhar kiran wrote:i think this is your first post in this thread and even you didnt answer or reply to my post ,then why are you frustrated?

Because:
(a) Your reply shows just how much notice you take of responses (this is my 4th).
(b) What am I supposed to reply to? This thread has been meandering for the best part of a month; and I don't see any end in sight.

Why don't you simply state exactly what your problem is, and close threads when you get an answer?

Given the subject of this one, I can imagine it being open for as long as you've got "doubt in some program", which is NOT the intent of this site.

Winston

i mean is this mandatory when you get the answer for one doubt then going to close the thread,i already mention title as doubts in program and technical question in java, really its a very helpful forum
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jesper de Jong wrote:
sekhar kiran wrote:i think we dont have to declare another class in main method,iam not understanding about error message,declaring class in a method which refere to local class so thats why i tried here

Arbitrary statements, such as System.out.println(...), must always be in a method or constructor (or inside a static or instance initializer). You cannot put statements at class-level, which is what you were doing in your example. It doesn't matter that class AX is defined inside the main() method; the System.out.println(...) statement is not inside a method or constructor (or an initializer) in the AX class, so it's not allowed there.

i got it,let me confirm is that local class which i defined inside the method?i execute code now

previously i asked about you how the execution occurs,here i post the order which upto my knowledge if anything wrong correct me
class,static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program*/
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

sekhar kiran wrote:i got it,let me confirm is that local class which i defined inside the method?i execute code now

The class AX which you defined inside the main() method is a local class.

sekhar kiran wrote:class,static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program*/

I don't understand what you're trying to say here.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jesper de Jong wrote:

sekhar kiran wrote:class,static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program*/

I don't understand what you're trying to say here.

i mean when we run the program ,execution how it goes is that way correct
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

When you run a program, it starts with the main method in the class you list.

From there, it all depends on what is in your main.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
fred rosenberger wrote:When you run a program, it starts with the main method in the class you list.

From there, it all depends on what is in your main.

i mean first class will be loaded after that static variable and static method will be initialized then it goes to main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

I stand by my statement. You go to main() first, and then it depends. Example:


In this example, the print statement on line 7 executes first, and then the static block runs.

However, if you switch lines 7 and 8:

The static block runs first. But in both cases, you start with main, and depending on what is there, different things run.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
fred rosenberger wrote:I stand by my statement. You go to main() first, and then it depends. Example:


In this example, the print statement on line 7 executes first, and then the static block runs.

However, if you switch lines 7 and 8:

The static block runs first. But in both cases, you start with main, and depending on what is there, different things run.

i think it is instance block in 2nd line,is that static block or instance block
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

oops...sorry...you're talking about this?


I would suggest you try it yourself and see what happens.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
fred rosenberger wrote:oops...sorry...you're talking about this?


I would suggest you try it yourself and see what happens.

here i know it will execute first static block and goes to main method and print the statement,ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence
shyam sundar R
Greenhorn

Joined: Apr 14, 2013
Posts: 1
Strings are immutable objects .. output will be pqr .

Will Zelan wrote:
what is the output of the program

2)sorting array without third variable in below code i didnt understand logic anyone tell me
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

shyam sundar R wrote:Strings are immutable objects .. output will be pqr .


String immutability has nothing to do with it. The reason the output is pqr is because Java passes references by value.

And I believe this was covered about a bazillion posts ago. Please read the thread before posting, to avoid going over old ground.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7800
    
  21

sekhar kiran wrote:i mean is this mandatory when you get the answer for one doubt then going to close the thread

Pretty much: Yes.

You plainly haven't read the UseOneThreadPerQuestion (←click) page - or, for that matter, HowToAskQuestionsOnJavaRanch (←click).

Please do so now.

i already mention title as doubts in program and technical question in java

Hey, why stop there? Why not start a "Please tell me everything there is to know about Java" thread? You could keep that open for years.

Actually...don't.

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:here i know it will execute first static block and goes to main method and print the statement,


Ok, so you understand order of execution then? Good.

ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence


If you want such an example, you can easily create it yourself.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Winston Gutkowski wrote:
sekhar kiran wrote:i mean is this mandatory when you get the answer for one doubt then going to close the thread

Pretty much: Yes.

You plainly haven't read the UseOneThreadPerQuestion (←click) page - or, for that matter, HowToAskQuestionsOnJavaRanch (←click).

Please do so now.

i already mention title as doubts in program and technical question in java

Hey, why stop there? Why not start a "Please tell me everything there is to know about Java" thread? You could keep that open for years.

Actually...don't.

Winston

ok i have doubt about some other topic which are not related to execution,can i post here or can i create a new thread
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Other topic == new thread. It's not a difficult concept.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:
sekhar kiran wrote:here i know it will execute first static block and goes to main method and print the statement,


Ok, so you understand order of execution then? Good.

ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence


If you want such an example, you can easily create it yourself.

i will post the order which is understand upto my knowledge till now, if anything goes wrong inform me,here posting when the run the program the class wil be loaded after that static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:
sekhar kiran wrote:here i know it will execute first static block and goes to main method and print the statement,


Ok, so you understand order of execution then? Good.

ok i will say one example a program which consists of static variables,instance variables,static block,instance block,constructors,some methods and statements which order it execute sequence


If you want such an example, you can easily create it yourself.

i will post the order which is understand upto my knowledge till now,here posting when the run the program the class wil be loaded after that static variable,static block,main method->checking constructor if it exists invoking instance block,constructor and method else execute the main method statements and terminate the program ,ff anything goes wrong correct me
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
This correspondence now closed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: order of execution