Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

question about the class declaration.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have used java programming for a while.
the question is always there. Right now, I feel I have to make it clear.
for example.
public class A1{
static int gg = 1;
void aMethod1(){
}
void aMethod2(){

}
}
public class B1{

whatever method;
public static void main(String arg[]){
//part1
A1 aa = new A1();
aa.aMethod1();
System.out.println(String.toString(aa.gg));
//part2
A1 aa2;
aa.aMethod1();
System.out.println(String.toString(aa.gg));

}
}
question1:
part 1 and part 2, which one is correct?
Actually, I saw both of code in the book. It seems both of them
are correct.
question2:
if so, what is the deference?
question3:
if the declaration of aa.gg is not static and is not assigned 1.
what would happen?
thanks
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think any of the code will work . As the variable defined belongs to the class and not to the object . Static variable can be only excessed by the class and not by the object So If u are writing something like this .
class A1
{
static int gg = 1;
void aMethod1()
{
System.out.println("Class A1");
}
void aMethod2()
{
}
}//end of class
public class B1
{
public static void main(String arg[])
{
A1 aa = new A1();
aa.aMethod1();
String s1=String.valueOf(A1.gg);
System.out.println(A1.gg);
System.out.println(s1);
A1 aa2;
aa.aMethod1();
System.out.println(A1.gg);
}
}
this will definately print the value as 1
------------------
Sandeep Jain
 
Cq
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUOTE]Originally posted by Sandeep Jain:
public static void main(String arg[])
{
A1 aa = new A1();
aa.aMethod1();
String s1=String.valueOf(A1.gg);
System.out.println(A1.gg);
System.out.println(s1);
A1 aa2;
[strong]
changeaa.aMethod1(); ==> aa2.aMethod1();
changeSystem.out.println(A1.gg); ==>
System.out.println(aa2.gg)
[/strong]
}
}

what is the result?
I ask the question because I am writing C++ program using
observer and mediator pattern.
below are c++
I find both ways are workable.
A1 aa;
or
A1 aa = new A1();
And I am pretty sure I saw both ways in java.
So I want to know it exactly.
thanks
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case everything works OK because you have an A1 object and you chose to use the "object" form of accessing gg (aa.gg) vs the "class" form of accessing gg (A1.gg). It is usually preferred to use the A1.gg form because it explicitly identifies the variable as a class variable.
The code related to aa2 would cause a compile error because aa2 was "potentially not initialized" in the language of javac. In this case you would have to use the form A1.gg to access the static variable.
If aa2 was initialiazed, even to null, you could then access the static by using aa2.gg.
You could also just have code in B1's main that said:
System.out.println(A1.gg);
since you don't need an instance of the class to access a class variable.
Did this answer your question?
 
Cq
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,thanks a lot.
So you mean if I put static or final before the declaration of
variable, it is class form variable.
whatever I initialize the class, it is one copy in the memory.
So I can regard it as a global variable. if I have aa1 = new A1();
and aa2 = new A1(). aa1.gg and aa2.gg locate at the same memory.
changing aa1.gg, aa2.gg and A1.gg are changed as well.
Am I correct?
 
reply
    Bookmark Topic Watch Topic
  • New Topic