Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

LOCAL CLASS

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the code given below how do i create an instance of the LocalClass so that i can invoke the method lc() which belongs to the LocalLCass?


Source:self

Thank you in advance.
Srikanth Iyer.

code:

import java.io.*;

interface B { String i ="3";void b();}
interface A extends B{ static String i="5"; void a();}

class Check implements A
{
public static void main(String args[])
{
String i ="1";
Check c = new Check();
c.b();
c.a();
System.out.println(i+" "+"main");
}
public void b(){System.out.println(i+" "+"bubu");}
public void a(){System.out.println(i+" "+"b");}
}



class C{
public void ca()
{
class LocalClass extends C{ String i = "2"; void lc() {System.out.println(i+" "+"lc");}}
}
}
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can only instantiate LocalClass inside the method ca of class C, but after the class definition. It is not visible from outside method or say it like its
scope is limited to method only.

You can create an instance of class C in the main method and then call the ca() method. And inside the ca method, after LocalClass definition,

new LocalClass().lc();



Thanks,
 
Srikanth Iyer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I invoke the method lc in the main method???
Thanks in advance.
 
Srikanth Iyer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if i have to use the object of the local calss should i implement an interface or override the behavior of its super type.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can't create an object of the local class outside the scope of that method in which the class is defined.

What I said in my previous post:




Thanks,
[ May 09, 2007: Message edited by: Chandra Bhatt ]
 
I brought this back from the farm where they grow the tiny ads:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic