• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this a Java Program?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I know if this is correct?
class MyClass{
int a
MyClass(){
this.a=69;
}
void MyMethod(){
printf("a is"+this.a);
this.a=this.a*this.a;
printf("now a is "+this.a);
}
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Your program is incorrect. I think what your are trying to do is something like this. Try out this program
public class MyClass
{
int a;
MyClass(){
this.a=10;
}
public void method()
{
System.out.println("a is" + this.a);
this.a= this.a* this.a;
System.out.println("now a is "+ this.a);
}
public static void main(String []args)
{
MyClass myobj = new MyClass();
myobj.method();
}
}
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or,
<PRE>
public class MyClass
{
int a;
MyClass(){
a=10;
}
public void myMethod(){
System.out.println("a is" + a);
a*=a;
System.out.println("now a is "+ a);
}
public static void main(String []args)
{
MyClass myobj = new MyClass();
myobj.myMethod();
}
}

</PRE>
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"WhoLetsTheDogsOut",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements. This user name has now been disabled.
Thanks.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somewhat more instructive is the following code:
public class MyClass
{
int a;
MyClass(){
a=10;
}
public void method()
{
int a = 5;
System.out.println("a is" + a);
a=a*this.a;
System.out.println("now a is "+ a);
}
public static void main(String []args)
{
MyClass myobj = new MyClass();
myobj.method();
}
}
The variable a that is redefined in method has precedence over the instance method defined in the class. Using this.a is needed to use the instance variable rather than the method variable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic