Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. class aaa{
  2. public void method{
  3. String value="as some output";
  4. string other="it has some output";
  5. }
  6. }
  7. public static void main(String args[]){
  8. aaa obj=new first();
  9. bbb object=new second();
  10. }
  11.  
  12. class bbb{
  13. aaa obj2=new aaa();
  14. System.out.println(obj2.value); //It gives error here also
  15. }
  16.  
  17. // use capitals for the first letter, conventions are good ;)
  18. public class Aaa{
  19. // I think this is what you meant, you want member variables, add public if you
  20. // want them to be accessible anywhere
  21. public String value="as some output";
  22. public String other="it has some output";
  23.  
  24. // missing brackets
  25. public void method() {
  26. // do something/anything
  27. }
  28. }
  29.  
  30. public class Bbb{
  31. // you need to put this in a main... without getting exceptions
  32. public static void main(String args[]){
  33. Aaa obj2=new Aaa();
  34. // now you can access the field value, since it's public
  35. System.out.println(obj2.value); //error should be gone now
  36. }
  37. }
  38.  
  39. public class TestMain{
  40. public static void main(String args[]){
  41. // first and second aren't classes, you meant Aaa and Bbb?
  42. Aaa objA=new Aaa();
  43. Bbb objB=new Bbb();
  44. }
  45. }
  46.  
  47. class Aaa
  48. {
  49. //...
  50. public String getValue()
  51. {
  52. return this value;
  53. }
  54. }
  55.  
  56. //...
  57. Aaa a = new Aaa();
  58. String test = a.getValue();
  59. //...
  60.  
  61. class Foo
  62. {
  63. // ...
  64. public String value = "bar";
  65. //...
  66. }
  67.  
  68. //...
  69. Aaa a = new Aaa();
  70. String test = a.value;
  71. //...
  72.  
  73. // note that I've renamed classes to follow the convention of uppercasing class names.
  74. // this makes the code much easier to read.
  75. class Aaa {
  76. public String value = "instance value initialized when the class loads (first use)";
  77. public String other = null;
  78.  
  79. // a method declaration must have parentheses, even if it takes no parameters
  80. public void method() {
  81. other = "instance value, initially null, set by calling method";
  82. }
  83. }
  84.  
  85. class Bbb {
  86. Aaa aaaInBbb = new Aaa();
  87.  
  88. public void method(){
  89. // every statement (except variable declarations) must be in a method
  90. System.out.println(aaaInBbb.value); // access the public value in aaaInBbb
  91. }
  92. }
  93.  
  94. class C {
  95. // note that main(...) must be in a class as well - as all methods in Java must
  96. public static void main(String[] args) { // convention also puts [] next to the type
  97. Aaa aaa = new Aaa(); // this variable is never used.
  98. Bbb bbb = new Bbb();
  99.  
  100. bbb.method(); // causes instance of Bbb to print out aaaInBbb.value
  101. }
  102. }
  103.  
  104. class aaa{
  105. static String value;
  106. public void method{
  107. value="as some output";
  108. }
  109. }
  110.  
  111. class bbb extends aaa{
  112. aaa obj2=new aaa();
  113. obj2.method();
  114.  
  115. System.out.println(value); //Output is "as some output";
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement