GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes doubt in some programs and question in java 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

doubt in some programs and question in java

sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
sekhar kiran wrote:the basic thing whenever calling method should be semicolon,am i right


Jeff Verdegan wrote:I already answered that question twice. I'm not answering it again.

ok i think it is basic thing we have to put semicolon during method call unless some restrictions

Jeff Verdegan wrote:I already did for some of them. I'm not going to repeat myself. For the others, I'm going to leave that as an exercise for you.

If you want to learn, you need to be a more active participant in your own education, rather than just sitting back helplessly, waiting for people to shove information down your throat.

oh ok i will try



Jeff Verdegan wrote:The error message is telling you what the problem is. If you don't understand the error and you want somebody here to help you with it, then copy/paste the exact, complete error message here, and indicate clearly which line is causing it.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11350
    
  16

sekhar kiran wrote:


So i'm coming to this thread a little late, and don't plan on reading the other 100+ posts...but...
This error looks fairly straightforward.

Do you have a method named "a" that takes two integer types in this class?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:
Jeff Verdegan wrote:The error message is telling you what the problem is. If you don't understand the error and you want somebody here to help you with it, then copy/paste the exact, complete error message here, and indicate clearly which line is causing it.



So what don't you understand?

methodcall.java:13

There are two pieces of information there. Do you understand them?


cannot find symbol

There are 3 words there. Do you understand all of them?


symbol : method a(int,int)

There are a couple of pieces of information there that provide more detail about the previous line. Do you understand them?



location: class methodcall
a(5,3);

There's some very clear and specific information there. Do you understand it?


Honestly, which specific parts of that very short and precise error message do you not understand? Did you try reading it and thinking about it?
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
fred rosenberger wrote:
So i'm coming to this thread a little late, and don't plan on reading the other 100+ posts...but...
This error looks fairly straightforward.

Do you have a method named "a" that takes two integer types in this class?

yeah i declared method a see below
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:
fred rosenberger wrote:
So i'm coming to this thread a little late, and don't plan on reading the other 100+ posts...but...
This error looks fairly straightforward.

Do you have a method named "a" that takes two integer types in this class?

yeah i declared method a see below



Look at this part of the error message again:

symbol : method a(int,int)
location: class methodcall


It says that the method a(int,int) does not exist in the class methodcall.

You have to hae a reference to an instance of methodDeclaration in order to call its a() method, just like you have to have an instance of PrintStream (such as the instance refererred to by System.out) in order to call its println() method.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:Look at this part of the error message again:

symbol : method a(int,int)
location: class methodcall


It says that the method a(int,int) does not exist in the class methodcall.

You have to hae a reference to an instance of methodDeclaration in order to call its a() method, just like you have to have an instance of PrintStream (such as the instance refererred to by System.out) in order to call its println() method.

yeah, i created object for class and call the method using object then it works

actually i need to know how the program exection process that is java does not execute line by line process,whenever we run the program it goes to main method after that how the proecess will goes
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:actually i need to know how the program exection process that is java does not execute line by line process,whenever we run the program it goes to main method after that how the proecess will goes


It enters main, and it executes whatever is in main, in order, except as affected by branching (if, switch/case), looping (for, while, do/while), constructors, or method calls. You can see that in your own program.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:
sekhar kiran wrote:actually i need to know how the program exection process that is java does not execute line by line process,whenever we run the program it goes to main method after that how the proecess will goes


It enters main, and it executes whatever is in main, in order, except as affected by branching (if, switch/case), looping (for, while, do/while), constructors, or method calls. You can see that in your own program.

can you tell me the order for my previous program by mention the numbers how it execute program,it is helpful to me and easily understand
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:can you tell me the order for my previous program by mention the numbers how it execute program,it is helpful to me and easily understand


I would think you could figure that out for yourself just by looking at the code. It's an extremely short program, and a little bit of reasoning combined with what you already know about Java should make it obvious.

But if you can't figure it out, or if you just want confirmation of your reasoning, then add a System.out.println right before and/or after every line of your current program that tells you where you are. For example:
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
yeah i got an error

methodcall.java:3: <identifier> expected
System.out.println("before going to a method");
^
methodcall.java:3: illegal start of type
System.out.println("before going to a method");
^
2 errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You can't put statements just any old place. They have to go inside a method, constructor, static initializer block, or instance initializer block.

The place where you're trying to put that println() is not a line that gets executed.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:You can't put statements just any old place. They have to go inside a method, constructor, static initializer block, or instance initializer block.

The place where you're trying to put that println() is not a line that gets executed.

ok i will check it

actually if we doesnot provid any value to instance variable and static variable it automatically assigned default value right,here below program not assing default values

here b is local variable or instance variable?how to print default values
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
sekhar kiran wrote: . . . here a is local variable or instance variable? . . .
No, there is no such value as a in your code at all. There is a class with the poorly chosen name a, but no value a.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You will probably find something about default values in this section of the Java™ Tutorials.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Campbell Ritchie wrote:
sekhar kiran wrote: . . . here a is local variable or instance variable? . . .
No, there is no such value as a in your code at all. There is a class with the poorly chosen name a, but no value a.

sorry its b

Campbell Ritchie wrote:You will probably find something about default values in this section of the Java™ Tutorials.


compile time error
l.java:6: variable a might not have been initialized
System.out.println(a);
^
1 error

here my doubt is int b is alocal variable or instance variable?if we doesnot provide any values for instance and static it automatically assigned default values,how to get them,for example int has 0 ,String - null,boolean - false
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Here b is a local variable as it is declared locally inside a method. You are not allowed to use a local variable without initializing it like you have done. Hence, the compiler is complaining.


~ Mansukh
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Mansukhdeep Thind wrote:Here b is a local variable as it is declared locally inside a method. You are not allowed to use a local variable without initializing it like you have done. Hence, the compiler is complaining.

thanks for reply, ok i had also think it is local variable,for local variable we have to initialize value,how to declare instance variable and how to get default values for instance variable and static variable
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Please read this tutorial. Try and understand the different types of Java variables. If you have a doubt, revert back.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Mansukhdeep Thind wrote:Please read this tutorial. Try and understand the different types of Java variables. If you have a doubt, revert back.

i think here a is an instance variable,but how to execute the program and print default value of instance variable

l.java:4: <identifier> expected
System.out.println(a);
^
l.java:4: <identifier> expected
System.out.println(a);
^
2 errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

sekhar kiran wrote:i think here a is an instance variable,but how to execute the program and print default value of instance variable...

Sekhar,

This thread now includes 90+ posts, and we're still not sure if you understand the fundamentals of Java or not. You shouldn't "think" or "wonder" if something is so - you should know.

And the only way to know it is to read about it and test it. As Jeff said earlier: "participate in your own education".

Simply machine-gunning questions to forums like this - no matter how good (and I like to think we are) - will get you nowhere.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
That will not compile because you have a statement outside any methods.

And don’t go back and correct posts after they have been replied to. That makes the replies look like nonsense.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

sekhar kiran wrote:
Mansukhdeep Thind wrote:Please read this tutorial. Try and understand the different types of Java variables. If you have a doubt, revert back.

i think here a is an instance variable,but how to execute the program and print default value of instance variable

l.java:4: <identifier> expected
System.out.println(a);
^
l.java:4: <identifier> expected
System.out.println(a);
^
2 errors


I agree with Winston. Please grab a copy of Head First Java and study the fundamentals of the language first. No point firing arrows in the dark. Be thorough with at least the following concepts:

a) Declarations and Access control

b) OOPS concepts (abstraction, inheritance, polymorphism)

c) Constructors

d) Flow control and iteration constructs

e) Exceptions, checked and unchecked

f) String class and its APIs

That is the only way you will improve your level of understanding and ask better questions.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
ha ok ,the below program is wrong i know the statement does not executes
class l
{
int a;
System.out.println(a);
public static void main(String args[])
{

}

but if i provide static variable i can able to get default value,here below program which gives output 0 which is default value for static variable, in the same way how to get instance variable default value

},let me tell you if i dont know basic about java then how can i tell the static variable assign defaul values and instance variable provide default value and all primitve data type had default values,for example i will tell, i know instance variable provide default values if we not assinged any value,but i dont know practically how to get them like that,so that why iam here to clear my doubts ,but in this forum i would like to say thanks to all of them especially Jesper de Jong , Jeff Verdegan, which they are helping me to understand

Campbell Ritchie wrote:That will not compile because you have a statement outside any methods.

And don’t go back and correct posts after they have been replied to. That makes the replies look like nonsense.

i didnt correct or edit anything in my previous posts ok
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

sekhar wrote: but if i provide static variable i can able to get default value,here below program which gives output 0 which is default value for static variable, in the same way how to get instance variable default value


No. That is not correct. static is a keyword in java which marks that particular member as belonging to a class rather than an instance. And the 0 that you see is because it is an int type, which is by default initialized to 0. I feel I am spoon feeding you. You need to devote more time to self study.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11350
    
  16

These two examples aren't equivalent:




or rather...there are TWO differences. In the first example, you have a member variable and an executable statement outside a method.
In the second, you have a static member variable, and NO executable outside a method.

The best way to experiment is to change exactly ONE thing...either make 'a' static, OR move the println statement. If you do two (or more) things, you don't know what caused what.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

sekhar kiran wrote:but if i provide static variable i can able to get default value

<sarcasm>
Right. So that's plainly the solution: make everything static.
</sarcasm>

Winston
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
fred rosenberger wrote:These two examples aren't equivalent:




or rather...there are TWO differences. In the first example, you have a member variable and an executable statement outside a method.
In the second, you have a static member variable, and NO executable outside a method.

The best way to experiment is to change exactly ONE thing...either make 'a' static, OR move the println statement. If you do two (or more) things, you don't know what caused what.

yeah you are correct both are not equivalent,i know first program is wrong because println statement is outside the method its wrong,in 2nd program static variable gets default value by using print method,k let me confirm first if we doesnot provide any values to instance variable will they assign default value automatically like int ->0,String->null,boolean->false,char->null,byte->0 ,is it right or wrong,
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Mansukhdeep Thind wrote:
sekhar wrote: but if i provide static variable i can able to get default value,here below program which gives output 0 which is default value for static variable, in the same way how to get instance variable default value


No. That is not correct. static is a keyword in java which marks that particular member as belonging to a class rather than an instance. And the 0 that you see is because it is an int type, which is by default initialized to 0. I feel I am spoon feeding you. You need to devote more time to self study.


upto my knowledge which iam studied about java,static variables and instance variable are different,static int a means it refers to static variables,int a means it is instance variable, If you do not specify an initial value to an instance & Static variable a default value will be assigned automatically,i know static is keyword in java which is also refere as class variables it can initialized first and only once ,we dont want to create objects etc etc,i think your are not getting my question that why you mentiond your spoon feeding me
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:let me confirm first if we doesnot provide any values to instance variable will they assign default value automatically


Default values are given to both instance variables (non-static member variables) and class variables (static member variables). This happens whether we initialize them or not.


like int ->0,String->null,boolean->false,char->null,byte->0 ,is it right or wrong,


Close. More specifically:

byte: 0
short: 0
char: 0 (not null--char is a primitive so it cannot be null)
int: 0
long: 0
float: 0.0
double: 0.0
boolean: false
all references: null
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:
upto my knowledge which iam studied about java,static variables and instance variable are different,static int a means it refers to static variables,int a means it is instance variable,


Yes.

A static member variable, also called a class variable, is associated with the class as a whole, not with any specific instance. There is one copy for the entire class.

A non-static member varaible, also calle an instance variable, is associated with an individual instance of that class (an object). Each instance has its own copy.

i know static is keyword in java which is also refere as class variables it can initialized first and only once


Well, like any variable, it can be initialized only once. However, it can be set as many times as we want. Unless it's final.

,we dont want to create objects


That's not why we use static methods and member variables. We don't try to avoid creating objects. The whole point of an object-oriented language is that a lot of what you want to express is accomplished by using--surprise!--objects.

So when we use static members, it's not because we're trying to avoid creating an object; it's just that an object doesn't make design sense in that situation.

sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
Jeff Verdegan wrote:
sekhar kiran wrote:let me confirm first if we doesnot provide any values to instance variable will they assign default value automatically


Default values are given to both instance variables (non-static member variables) and class variables (static member variables). This happens whether we initialize them or not.


like int ->0,String->null,boolean->false,char->null,byte->0 ,is it right or wrong,


Close. More specifically:

byte: 0
short: 0
char: 0 (not null--char is a primitive so it cannot be null)
int: 0
long: 0
float: 0.0
double: 0.0
boolean: false
all references: null

previously i know about default values for primitive data types,but i studied char has null see char 0 to 2^7 –1 -> null character (or) ‘\u 0000’ is this wrong?
ok practically how to get default values in program without initialize any values

Jeff Verdegan wrote:That's not why we use static methods and member variables. We don't try to avoid creating objects. The whole point of an object-oriented language is that a lot of what you want to express is accomplished by using--surprise!--objects.

So when we use static members, it's not because we're trying to avoid creating an object; it's just that an object doesn't make design sense in that situation.

yeah ok,i mean what iam trying to say means static variable can be accesed before any object is created,at the starting of program static variable and static block will be initialize once,what happens if we give final to static
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:
previously i know about default values for primitive data types,but i studied char has null see char 0 to 2^7 –1 -> null character (or) ‘\u 0000’ is this wrong?


The "null character" is completely different from Java's null reference value. "Null character" is just a name given to the char with value 0. It's still a simple numeric primitive, and it has nothing to do with the null value that's used for references.

ok practically how to get default values in program without initialize any values


I don't understand the question. Member variables are always given their default values before we get to touch them. If we don't initialize them explicitly at declaration, they retain those values until we set a value elsewhere.

yeah ok,i mean what iam trying to say means static variable can be accesed before any object is created,at the starting of program static variable and static block will be initialize once


Static variables are initialized and static blocks are executed when the class is initialized. Since classes are loaded on-demand in Java, and not all at the start of the program, most statics will not be initialized at startup. They won't even exist.

,what happens if we give final to static


Exactly the same thing as if you make any other varialbe final: It can only have its value set once.

Additionally, final static variables have the requirement that they must be initialized at declaration or in a static initializer block in the class that declares them.
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84

Jeff Verdegan wrote:The "null character" is completely different from Java's null reference value. "Null character" is just a name given to the char with value 0. It's still a simple numeric primitive, and it has nothing to do with the null value that's used for references.

ok so char also default value is 0


Jeff Verdegan wrote:
I don't understand the question. Member variables are always given their default values before we get to touch them. If we don't initialize them explicitly at declaration, they retain those values until we set a value elsewhere.

even static variable also get default values if we donot specify anything to that variable,so the same way how can we get instance variable default value,if i provide int a in 6th line it will be treated as local variable ,so for local variable we have specify values otherwise it will compile time error



Jeff Verdegan wrote:
Static variables are initialized and static blocks are executed when the class is initialized. Since classes are loaded on-demand in Java, and not all at the start of the program, most statics will not be initialized at startup. They won't even exist.

so most static will no be initialise at start up and wont even exist means then how it can be initialized and why it is not initialising


Jeff Verdegan wrote:
Exactly the same thing as if you make any other varialbe final: It can only have its value set once.

Additionally, final static variables have the requirement that they must be initialized at declaration or in a static initializer block in the class that declares them.
so it means its values doesnot change
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:even static variable also get default values if we donot specify anything to that variable,so the same way how can we get instance variable default value,if i provide int a in 6th line it will be treated as local variable ,so for local variable we have specify values otherwise it will compile time error


Correct. Local variables do not have default values. We have to explicitly assign a value to them before we can read them.

so most static will no be initialise at start up and wont even exist means then how it can be initialized and why it is not initialising


They come into existence and get initialized when the class that declares them is loaded, which in turn happens the first time that class is used.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14192
    
  20

sekhar kiran wrote:even static variable also get default values if we donot specify anything to that variable,so the same way how can we get instance variable default value,if i provide int a in 6th line it will be treated as local variable ,so for local variable we have specify values otherwise it will compile time error


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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
sekhar kiran
Ranch Hand

Joined: Apr 25, 2013
Posts: 84
sekhar kiran wrote:even static variable also get default values if we donot specify anything to that variable,so the same way how can we get instance variable default value,if i provide int a in 6th line it will be treated as local variable ,so for local variable we have specify values otherwise it will compile time error


Jeff Verdegan wrote:Correct. Local variables do not have default values. We have to explicitly assign a value to them before we can read them.

then how to print default values of instance variable if we donot provide values

sekhar kiran wrote:so most static will no be initialise at start up and wont even exist means then how it can be initialized and why it is not initialising


Jeff Verdegan wrote:They come into existence and get initialized when the class that declares them is loaded, which in turn happens the first time that class is used.

can you give any example which understands clearly
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran wrote:then how to print default values of instance variable if we donot provide values


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



sekhar kiran wrote:so most static will no be initialise at start up and wont even exist means then how it can be initialized and why it is not initialising


Jeff Verdegan wrote:They come into existence and get initialized when the class that declares them is loaded, which in turn happens the first time that class is used.

can you give any example which understands clearly

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

sekhar kiran,
Your post was moved to a new topic.

The remainder of this thread has been split off to a new one as it is now a different question.

I'm locking this thread.
 
GeeCON Prague 2014
 
subject: doubt in some programs and question in java